3

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.

Juicy Scripter
  • 25,778
  • 6
  • 72
  • 93

2 Answers2

0

Not really sure what your trying to achieve here, but maybe this can help you out.

Poster extension for Firefox: http://www.google.nl/codesearch#1Ekgj8MZCE0/README&q=Firefox%20extension&l=9&ct=rc&cd=6

It seems to be doing what you need

tryme
  • 81
  • 3
0

You can convert it to a "conventional" array like this:

var dataArray = Array.prototype.slice.call(data);

This will allow you to write out that data:

bstm.writeByteArray(dataArray, dataArray.length);

Supposedly, this conversion will become unnecessary in Firefox 11, you will be able to pass in Uint8Array directly.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126