Alexander Demianenko
Alexander Demianenko

Reputation: 124

Read response stream via XMLHttpRequest

I have the code that should read response stream and fill progress bar. Server response is correct. The issue is updateProgress event is not fired.

function load_binary_resource(url) {
    var req = new XMLHttpRequest();
    req.open('GET', url, false);
    req.addEventListener("progress", updateProgress);
    req.overrideMimeType('text\/plain; charset=x-user-defined');
    req.send(null);
    if (req.status != 200) return '';
    return req.responseText;
}
function updateProgress (oEvent) {
    if (oEvent.lengthComputable) {
        console.log('loading');
        var percentComplete = oEvent.loaded / oEvent.total * 100;
        // ...
    } else {
        console.log('something happening');
    }
}

Upvotes: 1

Views: 1661

Answers (1)

David R
David R

Reputation: 15637

Try changing your,

req.addEventListener("progress", updateProgress);

as,

req.upload.addEventListener('progress', updateProgress, false);

Progress events exist for both download and upload transfers. The download events are fired on the XMLHttpRequest object itself. The upload events are fired on the XMLHttpRequest.upload object as per MDN Documentation

Hope this helps!

Upvotes: 2

Related Questions