If your WebDAV server is OK with just put (sans locks)...
I'd just use XMLHttpRequest
, as it provides a higher-level API than raw streams (and also avoids some performance/snappiness pitfalls). Just prepare use something like 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).
And where aContext
is the context argument (2nd) passed to asyncOpen. Since we're at it: You cannot really pass a string there. null
would be OK (let your listener keep track of stuff).