You can use FileReader to read the Blob as an ArrayBuffer.
Here's a short example:
var arrayBuffer;
var fileReader = new FileReader();
fileReader.onload = function(event) {
arrayBuffer = event.target.result;
};
fileReader.readAsArrayBuffer(blob);
Here's a longer example:
// ArrayBuffer -> Blob
var uint8Array = new Uint8Array([1, 2, 3]);
var arrayBuffer = uint8Array.buffer;
var blob = new Blob([arrayBuffer]);
// Blob -> ArrayBuffer
var uint8ArrayNew = null;
var arrayBufferNew = null;
var fileReader = new FileReader();
fileReader.onload = function(event) {
arrayBufferNew = event.target.result;
uint8ArrayNew = new Uint8Array(arrayBufferNew);
// warn if read values are not the same as the original values
// arrayEqual from: http://stackoverflow.com/questions/3115982/how-to-check-javascript-array-equals
function arrayEqual(a, b) { return !(a<b || b<a); };
if (arrayBufferNew.byteLength !== arrayBuffer.byteLength) // should be 3
console.warn("ArrayBuffer byteLength does not match");
if (arrayEqual(uint8ArrayNew, uint8Array) !== true) // should be [1,2,3]
console.warn("Uint8Array does not match");
};
fileReader.readAsArrayBuffer(blob);
fileReader.result; // also accessible this way once the blob has been read
This was tested out in the console of Chrome 27—69, Firefox 20—60, and Safari 6—11.
Here's also a live demonstration which you can play with: https://jsfiddle.net/potatosalad/FbaM6/
Update 2018-06-23: Thanks to Klaus Klein for the tip about event.target.result versus this.result
Reference:
- https://developer.mozilla.org/en-US/docs/Web/API/FileReader#readAsArrayBuffer()
- https://www.w3.org/TR/FileAPI/#dfn-readAsArrayBuffer
You can use FileReader to read the Blob as an ArrayBuffer.
Here's a short example:
var arrayBuffer;
var fileReader = new FileReader();
fileReader.onload = function(event) {
arrayBuffer = event.target.result;
};
fileReader.readAsArrayBuffer(blob);
Here's a longer example:
// ArrayBuffer -> Blob
var uint8Array = new Uint8Array([1, 2, 3]);
var arrayBuffer = uint8Array.buffer;
var blob = new Blob([arrayBuffer]);
// Blob -> ArrayBuffer
var uint8ArrayNew = null;
var arrayBufferNew = null;
var fileReader = new FileReader();
fileReader.onload = function(event) {
arrayBufferNew = event.target.result;
uint8ArrayNew = new Uint8Array(arrayBufferNew);
// warn if read values are not the same as the original values
// arrayEqual from: http://stackoverflow.com/questions/3115982/how-to-check-javascript-array-equals
function arrayEqual(a, b) { return !(a<b || b<a); };
if (arrayBufferNew.byteLength !== arrayBuffer.byteLength) // should be 3
console.warn("ArrayBuffer byteLength does not match");
if (arrayEqual(uint8ArrayNew, uint8Array) !== true) // should be [1,2,3]
console.warn("Uint8Array does not match");
};
fileReader.readAsArrayBuffer(blob);
fileReader.result; // also accessible this way once the blob has been read
This was tested out in the console of Chrome 27—69, Firefox 20—60, and Safari 6—11.
Here's also a live demonstration which you can play with: https://jsfiddle.net/potatosalad/FbaM6/
Update 2018-06-23: Thanks to Klaus Klein for the tip about event.target.result versus this.result
Reference:
- https://developer.mozilla.org/en-US/docs/Web/API/FileReader#readAsArrayBuffer()
- https://www.w3.org/TR/FileAPI/#dfn-readAsArrayBuffer
The Response API consumes a (immutable) Blob from which the data can be retrieved in several ways. The OP only asked for ArrayBuffer, and here's a demonstration of it.
var blob = GetABlobSomehow();
// NOTE: you will need to wrap this up in a async block first.
/* Use the await keyword to wait for the Promise to resolve */
const arrayBuffer = await new Response(blob).arrayBuffer();
Alternatively you could use this:
new Response(blob).arrayBuffer().then((arrayBuffer) => {
// do something with the arrayBuffer
});
Note: This API isn't compatible with older (ancient) browsers so take a look to the Browser Compatibility Table to be on the safe side ;)
Blobs and arrayBuffers - JavaScript - SitePoint Forums | Web Development & Design Community
javascript - ArrayBuffer to blob conversion - Stack Overflow
[AskJS] Webworkers: passing blobs faster than passing ArrayBuffers as transferable in Chrome
How do I extract a blob and it's array buffer, from a formidable variable, in node .js?
» npm install blob-to-arraybuffer
I don't know why the author did wrap his Uint8Array in a new one... note that I don't really know either the deprecated BlobBuilder API, but one typo I can see in your code is that you need to wrap your TypedArray in a normal Array:
new Blob([new Uint8Array(buffer, byteOffset, length)]);
The Blob() constructor takes a blobParts sequence as first parameter, and then searches for BufferSource, USVStrings and Blob elements in this sequence. So when you pass a TypedArray, it will actually iterate over all the entries of this TypedArray and treat these as USVString (and thus convert their numerical value to UTF-8 strings in the Blob). That's rarely what you want, so better always pass an Array in this constructor.
Note that if you don't need to slice the buffer, it's probably better to directly pass it instead of using an intermediary Uint8Array (but still in a normal Array):
new Blob([buffer]);
var buffer = new ArrayBuffer(32);
new Blob([buffer]);
so the Uint8Array should be
new Blob([new Uint8Array([1, 2, 3, 4]).buffer]);
» npm install blob-to-buffer
I'm running some tests in Chrome with webworker and I'm finding quite odd that passing blobs back and forth is way, way faster than ArrayBuffers.
This is the testing code I'm using with a 1Gb file:
ArrayBuffer:
const buffer = await fetch('./1GbFile.bin').then(data => data.arrayBuffer());
console.time("Buffer")
worker.onmessage = function(e) {
console.timeEnd("Buffer");
};
worker.onerror = function(e) {
reject(e.message);
};
worker.postMessage(buffer, [buffer]);Blob:
const blob = await fetch('./1GbFile.bin').then(data => data.blob());
console.time("Blob")
worker.onmessage = function(e) {
console.timeEnd("Blob");
};
worker.onerror = function(e) {
reject(e.message);
};
worker.postMessage(blob);And this is the webworker, it just returns the same data it receives:
self.onmessage = function(e) {
const data = e.data;
if (data instanceof ArrayBuffer)
self.postMessage(data, [data]);
else
self.postMessage(data);
}And the staggering results:
Buffer: 34.46484375 ms
Blob: 0.208984375 ms
I knew blob was very optimized in this scenario, but I thought using the transferable option would make it work somehow similar, but it's more than 100 times slower.
And the transferable option is definitely doing its thing, removing the option makes it like 10 times slower.
Edit: The same code is way faster in Firefox:
Buffer: 2ms
Blob: 0ms