how does axios handle blob vs arraybuffer as responseType?

From axios docs: // `responseType` indicates the type of data that the server will respond with // options are: ‘arraybuffer’, ‘document’, ‘json’, ‘text’, ‘stream’ // browser only: ‘blob’ responseType: ‘json’, // default ‘blob’ is a “browser only” option. So from node.js, when you set responseType: “blob”, “json”will actually be used, which I guess fallbacks to … Read more

How to go from Blob to ArrayBuffer

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]); … Read more

Convert a binary NodeJS Buffer to JavaScript ArrayBuffer

Instances of Buffer are also instances of Uint8Array in node.js 4.x and higher. Thus, the most efficient solution is to access the buf.buffer property directly, as per https://stackoverflow.com/a/31394257/1375574. The Buffer constructor also takes an ArrayBufferView argument if you need to go the other direction. Note that this will not create a copy, which means that … Read more

ArrayBuffer to base64 encoded string

function _arrayBufferToBase64( buffer ) { var binary = ”; var bytes = new Uint8Array( buffer ); var len = bytes.byteLength; for (var i = 0; i < len; i++) { binary += String.fromCharCode( bytes[ i ] ); } return window.btoa( binary ); } but, non-native implementations are faster e.g. https://gist.github.com/958841 see http://jsperf.com/encoding-xhr-image-data/6 Updated benchmarks: https://jsben.ch/wnaZC

Converting between strings and ArrayBuffers

Update 2016 – five years on there are now new methods in the specs (see support below) to convert between strings and typed arrays using proper encoding. TextEncoder The TextEncoder represents: The TextEncoder interface represents an encoder for a specific method, that is a specific character encoding, like utf-8, iso-8859-2, koi8, cp1261, gbk, … An … Read more