2011-11-07 42 views
4

我试图让通过下面的代码Ajax请求的进展情况:的XMLHttpRequest 2下载进度事件只发射一次

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(); 

的问题是,进度事件仅触发一次,负载前右事件(也就是说,在Webkit中,它似乎不能在Gecko下运行)。

我做错了什么,或者它只是不正确的支持?

+0

你在本地主机测试? – zatatatata

+0

是的,但远程主机给出了相同的结果。 – DADU

+2

也许这确实有帮助:http://stackoverflow.com/questions/76976/how-to-get-progress-from-xmlhttprequest – Fabian

回答

11

使用

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

(注意添加.upload

+3

OP在问题中提到“下载”,您的回答将提供关于上传部分的信息 – Ross