I'm writting a Firefox extension. Using javascript, I want to download a binary file from a web POST, and then I want write its contents into a file. My difficulty is how to convert from the type returned by the web to the type needed to write:
var c=new XMLHttpRequest();
c.responseType = "arraybuffer";
var data=Uint8Array(c.response);
"data" contains the binary contents. To write it to a file:
var file= FileUtils.getFile("ProfD", ["somefile"]);
var ostm= FileUtils.openFileOutputStream(file);
var bstm= Cc['@mozilla.org/binaryoutputstream;1'].createInstance(Ci.nsIBinaryOutputStream);
bstm.setOutputStream(ostm);
Then I need to write "data" in the "bstm", but the only way I have found to do it is very slow:
for(var i=0; i<data.length; i++)
bstm.write8(data[i]);
This works, but it's very slow for medium size files. Is there a better way to do it? Thanks.