1

I'm following a file specification to generate a binary file in javascript using blobs and arraybuffers. Everything was going well and I had the following blob:

var bb = new Blob([fileSig, version, numBlocks], {type: 'example/binary'});

The variables fileSig, version, and numBlocks are array buffers with the correct data in them. Now I've come to a point where I have a for loop that generates data which also needs to go into this blob.

I first thought I'd create an array of ArrayBuffers that gets populated as the for loop continued and then add it to the blob like:

var bb = new Blob([fileSig, version, numBlocks, arrayOfArrayBuffers], {type: 'example/binary'});

But the blob doesn't take it. Then I thought I'd run a loop on arrayOfArrayBuffers and append to the bb Blob, but Blobs don't allow appending.

Is there a way to append to an arrayBuffer, or Blob in this way? I need to use both Uint8Array, and Uint16Arrays.

Edit: a DataView seems to be what I need to use. I will keep a record of the offset, and then set the new data at that offset. I'll try it and post my findings.

garg
  • 1,246
  • 2
  • 16
  • 21
  • 1
    What about `[fileSig, version, numBlocks].concat(arrayOfArrayBuffers)`? – Bergi Oct 28 '13 at 10:49
  • Ah of course :) . Yes that would work. I just created an array eg arrayOfArrayBuffers. Pushed all new array buffers into it. and then finally did var bb = new Blob(arrayOfArrayBuffers, {type:'example/binary'}); That worked as needed. Much much simpler than what I was doing. – garg Oct 28 '13 at 16:56

0 Answers0