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 Uint16Array
s.
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.