admin管理员组

文章数量:1293159

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?

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?

Share Improve this question asked Nov 7, 2011 at 16:50 DADUDADU 7,0868 gold badges44 silver badges64 bronze badges 9
  • Are you testing in localhost? – zatatatata Commented Nov 7, 2011 at 17:02
  • Yes but the remote host gives the same result. – DADU Commented Nov 7, 2011 at 17:13
  • 2 Maybe this does help: stackoverflow./questions/76976/… – Fabian Commented Nov 7, 2011 at 17:18
  • @Fabian The answer, stackoverflow./questions/76976/… relies on PHP's readfile. I can't use that because I expect to receive an HTML image element as responseText instead of raw image data. – DADU Commented Nov 7, 2011 at 18:22
  • Just make sure your webserver is configured to send the content-length header. – Gerben Commented Nov 7, 2011 at 18:38
 |  Show 4 more ments

1 Answer 1

Reset to default 11

Use

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

(note the added .upload)

本文标签: javascriptXMLHttpRequest 2 download progress event only firing onceStack Overflow