I was studying Blobs, and I noticed that when you have an ArrayBuffer, you can easily convert this to a Blob as follows:
var dataView = new DataView(arrayBuffer);
var blob = new Blob([dataView], { type: mimeString });
The question I have now is, is it possible to go from a Blob to an ArrayBuffer?
You can use
FileReader
to read theBlob
as anArrayBuffer
.Here's a short example:
Here's a longer example:
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
versusthis.result
Reference:
Or you can use the fetch API
I don't know what the performance difference is, and this will show up on your network tab in DevTools as well.
Just to complement Mr @potatosalad answer.
You don't actually need to access the function scope to get the result on the onload callback, you can freely do the following on the event parameter:
Why this is better? Because then we may use arrow function without losing the context