0

I'm writing a handler for accessing binary data. Currently I'm doing this:

...
d = new FileReader();
d.onload = function (e) {
    var response_buffer, data, byte_len, temp_float;

    response_buffer = e.target.result;
    data = new DataView(response_buffer);
    byte_len = data.byteLength;

    // create empty placeholder for binary data received
    tmp_data = new Float32Array(byte_len / Float32Array.BYTES_PER_ELEMENT);
    len = tmp_data.length;

    // Pre-parse
    for (i = 0; i < len; i += 1) {
      tmp_data[i] = data.getFloat32(i * Float32Array.BYTES_PER_ELEMENT, true);
    }

    ...

Which works fine and "pre-processes" my fetched data (file/http) into my tmp_data array.

However I need to be able to handle large files as well (like 1GB+). My idea was to try and fetch only part of the file because from the binary structure I would know exactly what offsets I would have to fetch.

Question:
Is it possible to XHR for a "range" of bytes from a large file if I know offset and length instead of having to fetch the whole file?

Thanks!

frequent
  • 27,643
  • 59
  • 181
  • 333

2 Answers2

1

If your server respects it, you can set the Range: request header, which does exactly that.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Good idea. How about if I don't have control over the server? – frequent May 26 '14 at 18:54
  • It might respect that already (it is a standard header). If the server doesn't have any way to send the data you're looking for, you're out of luck. – SLaks May 26 '14 at 19:03
  • Ok. Let's poll some servers. If not supported I should get at least get a 501, so let's see. Thanks! – frequent May 26 '14 at 19:05
  • @frequent: No; it would be more likely to just ignore the header. Instead, check for the `Accept-Ranges` response header. – SLaks May 26 '14 at 19:07
1

You could also implement your own polling strategy where you ask for the data in chunks of x bytes and pass along an offset. Similar to how an endless scroll works on a web page.

TGH
  • 38,769
  • 12
  • 102
  • 135