[英]Add-on SDK: WebDAV upload from addon
I am trying to upload a file to a WebDAC server. 我正在尝试将文件上传到WebDAC服务器。 iLocalFile is a reference to a local input file:
iLocalFile是对本地输入文件的引用:
var inputStream = Cc["@mozilla.org/network/file-input-stream;1"]
.createInstance(Ci.nsIFileInputStream);
inputStream.init(iLocalFile, DELETE_ON_CLOSE, 0, 0);
var oIOService = Cc["@mozilla.org/network/io-service;1"]
.getService(Ci.nsIIOService)
var oHttpChannel = oIOService.newChannel(sRemoteFileName, "", null);
oHttpChannel.QueryInterface(Ci.nsIHttpChannel);
oHttpChannel.requestMethod = 'PUT';
oHttpChannel.QueryInterface(Ci.nsIUploadChannel);
oHttpChannel.setUploadStream(inputStream, 'application/pdf', -1);
var result = '';
console.log('Upload starting')
oHttpChannel.asyncOpen(in nsIStreamListener, result);
My problem is that I don't know how to implement the nsIStreamListener. 我的问题是我不知道如何实现nsIStreamListener。 I would be quite happy to thow away the response content if failure/success is still available.
如果失败/成功仍然存在,我很乐意放弃响应内容。
As per the selected answer with the following modification: 根据所选答案并进行以下修改:
var inputStream = Cc["@mozilla.org/network/file-input-stream;1"]
.createInstance(Ci.nsIFileInputStream);
inputStream.init(iLocalFile, -1, 0, 0);
xhr.send(inputStream);
If your WebDAV server is OK with just put (sans locks)... 如果您的WebDAV服务器可以正常使用(无锁)...
I'd just use XMLHttpRequest
, as it provides a higher-level API than raw streams (and also avoids some performance/snappiness pitfalls). 我只使用
XMLHttpRequest
,因为它提供了比原始流更高级别的API(并且还避免了一些性能/灵活性陷阱)。 Just prepare use something like xhr.send(File(iLocalFile.path))
. 只需准备使用
xhr.send(File(iLocalFile.path))
类的东西xhr.send(File(iLocalFile.path))
。
If you still want to use raw channels and a stream listener: 如果您仍然想使用原始频道和流监听器:
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
let listener = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIStreamListener]),
onStartRequest: function(aRequest, aContext) {
// request started
},
onDataAvailable: function(aRequest, aContext, aInputStream, aOffset, aSize) {
// responseData. May be called multiple times!
// You probably need to buffer the data from the input stream
// if you're interested in the response text and manually convert
// it afterwards.
// E.g. wrap in nsIBinaryInputStream and .readArrayBuffer()
// + TextDecoder in onStopRequest.
},
onStopRequest: function(aRequest, aContext, aStatusCode) {
// request done. aStatusCode is an nsresult, not the http code ;)
//You may want to check this with Components.isSuccessCode
}
};
Where aRequest
is the original or redirected channel QI'ed to nsIRequest
(you can QI it to nsIChannel
, etc. again). 其中
aRequest
是原始QI或重定向到nsIRequest
重定向通道(您可以再次将其QI到nsIChannel
等等)。 And where aContext
is the context argument (2nd) passed to asyncOpen. 其中
aContext
是传递给asyncOpen的上下文参数(第二个)。 Since we're at it: You cannot really pass a string there. 因为我们在这里:您不能真正在此处传递字符串。
null
would be OK (let your listener keep track of stuff). null
将是可以的(让您的听众跟踪内容)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.