【问题标题】:XMLHttpRequest 2 download progress event only firing onceXMLHttpRequest 2 下载进度事件只触发一次
【发布时间】:2011-12-23 19:02:50
【问题描述】:

我正在尝试通过以下代码获取 ajax 请求的进度:

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 下不起作用)。

是我做错了什么还是只是没有得到适当的支持?

【问题讨论】:

  • 你在本地测试吗?
  • 是的,但是远程主机给出了相同的结果。
  • @Fabian 答案,stackoverflow.com/questions/76976/… 依赖于 PHP 的 readfile。我不能使用它,因为我希望接收一个 HTML 图像元素作为 responseText 而不是原始图像数据。
  • 只要确保您的网络服务器配置为发送内容长度标头。

标签: javascript ajax html xmlhttprequest-level2


【解决方案1】:

使用

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

(注意添加的.upload

【讨论】:

  • OP 在问题中说“下载”,您的回答将提供有关上传部分的信息
猜你喜欢
  • 2012-09-25
  • 2013-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-15
  • 2011-02-27
相关资源
最近更新 更多