Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing already downloaded data

I need to download a large (>100MB) file of data via XmlHttpRequest. The data is from a third party and I would like to display the content gradually as it gets downloaded.

So I thought the following would work:

var req = new XMLHttpRequest();
req.open( "GET", mirror.url, true );
req.responseType = "arraybuffer";

req.onload = function( oEvent ) {
    console.log( "DONE" );
};
var current_offset = 0;
req.addEventListener("progress", function(event) {
    if( event.lengthComputable ) {
        var percentComplete = Math.round(event.loaded * 100 / event.total);
    }
    var data = req.response;
    // Fails here: req.response is null till load is called

    var dataView = new DataView( data );
    while( current_offset < dataView.byteLength ) {
        // do work
        ++current_offset;
    }

    console.log( "OFFSET " + current_offset + " [" + percentComplete + "%]" );

}, false);
try {
    req.send( null );
} catch( er ) {
    console.log( er );
}

Sadly, according to the spec, .response is not available.

Is there any way to access the already downloaded data without going to such horrible workarounds like using Flash?

EDIT:

Found at least a working non-standard solution for Firefox:

responseType = "moz-chunked-arraybuffer";

See also: WebKit equivalent to Firefox's "moz-chunked-arraybuffer" xhr responseType

like image 843
abergmeier Avatar asked Nov 04 '22 03:11

abergmeier


1 Answers

One solution would be to download only parts of the file using range requests to only download parts of the file. For more information see the following blog post HTTP Status: 206 Partial Content and Range Requests.

If you have control over the server you could also split up the file on the server side and download chunks of it. This way you would be able to access the response as the different chunks are recieved.

A third approach would be to use WebSockets to download the data.

If you have no control of the server you are downloading from and none of the other options will work, you will probably need to implement a proxy service that will act as an intermediate and allow you to download only part of it.

like image 170
TAS Avatar answered Nov 09 '22 03:11

TAS