Search code examples
javascriptstreambinaryxmlhttprequestarraybuffer

Partial XHR response reading for binary data, possible?


I am currently researching the possibility of reading partial XHR responses with binary data. Our current approach is based on the 'responseText' property and base64 encoding. Clearly, this is far from optimal.

How could we read partial Blob/ArrayBuffer responses using XHR? When I try in Chrome, the entire ArrayBuffer/Blob is made available when readyState = 4, but not before that.

To summarize, it seems to me that:

  • Reading XHR's responseText property: Responses can be read before readyState = 4, and we can stream base64 encoded binary data back to the client
  • Reading XHR's response property with responseType = 'arraybuffer': No partial response reading, but the entire buffer is made available when readyState = 4

Am I missing something here? What approach could we take to read partial binary responses?


Solution

  • Keep your eyes on the fetch API, currently supported by Firefox and Chrome.