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.

2
  • 1
    What about [fileSig, version, numBlocks].concat(arrayOfArrayBuffers)? Commented Oct 28, 2013 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. Commented Oct 28, 2013 at 16:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.