简体   繁体   English

使用新的SDK Firefox插件通过发布发送文件

[英]Send file via Post using new SDK Firefox Addon

I'm searching to send a zip file to a server using the "Request" class from the new Firefox SDK for addons. 我正在搜索使用新的Firefox SDK中的“请求”类将zip文件发送到服务器,以获取附加组件。 This is my code: 这是我的代码:

var Request = require("sdk/request").Request;
var file = new FileUtils.File(pathToZipFile);
Request({
   url: serverURL,
   content: file,
   onComplete: function (response) {
       for (var headerName in response.headers) {
          console.log(headerName + " : " + response.headers[headerName]);
       }
       console.log("Response " + response.text );
    }
}).post();

But the error is: 但是错误是:

[Exception... "Component returned failure code: 0x80520009 (NS_ERROR_FILE_INVALID_PATH) [nsILocalFile.target]" nsresult: "0x80520009 (NS_ERROR_FILE_INVALID_PATH)" location: "JS frame :: resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/commonjs/sdk/querystring.js :: stringify/< :: line 70" data: no] [异常...“组件返回的失败代码:0x80520009(NS_ERROR_FILE_INVALID_PATH)[nsILocalFile.target]” nsresult:“ 0x80520009(NS_ERROR_FILE_INVALID_PATH)”位置:“ JS框架::资源:// gre / modules / commonjs / toolkit / loader。 js-> resource://gre/modules/commonjs/sdk/querystring.js :: stringify / <::第70行”数据:否]

I have tried to do some checks and: 我试图做一些检查,并:

  1. The server is on and receives normal GET and POST without files 服务器已启动,并且可以接收没有文件的常规GET和POST
  2. The zip file is present and the path is right 该zip文件存在且路径正确

Do you see any errors? 看到错误了吗? Thanks a lot 非常感谢

The only way to do it with the Request module is to base a base64 encoded string to the content key. 使用“请求”模块执行此操作的唯一方法是将base64编码的字符串作为content密钥的基础。 If you don't use this, then you can send data such as a Blob or DOMFile ( new File() ) instance. 如果不使用它,则可以发送数据,例如BlobDOMFilenew File() )实例。

But as we see in the SDK code, the request module sends the data variable on request (if its not a HEAD or GET request). 但是,正如我们在SDK代码中看到的那样,请求模块根据请求发送data变量(如果它不是HEAD或GET请求)。

https://github.com/mozilla/addon-sdk/blob/master/lib/sdk/request.js#L110 https://github.com/mozilla/addon-sdk/blob/master/lib/sdk/request.js#L110

The data var is made by running stringify on anything passed to the content key: https://github.com/mozilla/addon-sdk/blob/master/lib/sdk/request.js#L76 通过对传递给content密钥的任何content运行stringify来制作data stringifyhttps : //github.com/mozilla/addon-sdk/blob/master/lib/sdk/request.js#L76

Stringify makes it a string: https://github.com/mozilla/addon-sdk/blob/f5fab7b242121dccfa4e55ac80489899bb9f2a41/lib/sdk/querystring.js#L30 Stringify使它成为字符串: https : //github.com/mozilla/addon-sdk/blob/f5fab7b242121dccfa4e55ac80489899bb9f2a41/lib/sdk/querystring.js#L30

So you have to send base64 encoded string. 因此,您必须发送base64编码的字符串。 Or a binary string. 或二进制字符串。 Which sucks. 真烂

You can use the sdk/io module to read the file as an ArrayBuffer and then turn that ArrayBuffer into a base64 string or binary string. 您可以使用sdk/io模块将其作为ArrayBuffer读取,然后将该ArrayBuffer转换为base64字符串或二进制字符串。

This shows how to get a binary string: https://stackoverflow.com/a/16365505/1828637 这显示了如何获取二进制字符串: https : //stackoverflow.com/a/16365505/1828637

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM