Page 149 - JavaScript
P. 149
Chapter 13: Binary Data
Remarks
Typed Arrays were originally specified by a Khronos editor's draft, and later standardized in
ECMAScript 6 §24 and §22.2.
Blobs are specified by the W3C File API working draft.
Examples
Converting between Blobs and ArrayBuffers
JavaScript has two primary ways to represent binary data in the browser.
ArrayBuffers/TypedArrays contain mutable (though still fixed-length) binary data which you can
directly manipulate. Blobs contain immutable binary data which can only be accessed through the
asynchronous File interface.
Convert a Blob to an ArrayBuffer (asynchronous)
var blob = new Blob(["\x01\x02\x03\x04"]),
fileReader = new FileReader(),
array;
fileReader.onload = function() {
array = this.result;
console.log("Array contains", array.byteLength, "bytes.");
};
fileReader.readAsArrayBuffer(blob);
6
Convert a Blob to an ArrayBuffer using a Promise (asynchronous)
var blob = new Blob(["\x01\x02\x03\x04"]);
var arrayPromise = new Promise(function(resolve) {
var reader = new FileReader();
reader.onloadend = function() {
resolve(reader.result);
};
reader.readAsArrayBuffer(blob);
});
arrayPromise.then(function(array) {
console.log("Array contains", array.byteLength, "bytes.");
});
https://riptutorial.com/ 106

