Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XMLHttpRequest 2 download progress event only firing once

I'm trying to get the progress of an ajax request via the following code:

var xhr = new XMLHttpRequest();


xhr.addEventListener('progress', function(event) {

    console.log(event.loaded / event.total);
},
false);

xhr.addEventListener('load', function() {

    console.log('load');
},
false);


xhr.open('get', 'test.php', true);
xhr.send();

The problem is, the progress event only fires once, right before the load event (that is, in Webkit, it doesn't seem to work under Gecko).

Am I doing something wrong or is it just not supported properly?

like image 748
DADU Avatar asked Nov 07 '11 16:11

DADU


1 Answers

Use

xhr.upload.addEventListener('progress', function(event) { ... });

(note the added .upload)

like image 197
Hernan S. Avatar answered Sep 25 '22 11:09

Hernan S.