[英]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: 我试图做一些检查,并:
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. 如果不使用它,则可以发送数据,例如
Blob
或DOMFile
( new 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
stringify
: https : //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.