HTML5文件上传带有多个进度条
我正在通过 XmlHTTPRequest 和 HTML5 上传多个文件。我的上传工作正常,但我希望每个文件上传都有一个进度条。然而,我的代码对所有文件上传使用最后一个进度条,而不是每次上传都使用自己的进度条。所以这在客户端主要是视觉上的,但这真的让我很恼火。由于某种原因,我假设附加文件上传进度的事件会覆盖自身并使用最后一个进度条。这是我的代码:
var files = event.dataTransfer.files;
// iterate over each file to upload, send a request, and attach progress event
for (var i = 0, file; file = files[i]; i++) {
var li = $("<li>" + file.name + "<div class='progressbar'></div></li>");
// add the LI to the list of uploading files
$("#uploads").append(li);
// fade in the LI instead of just showing it
li.hide().fadeIn();
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener('progress', function(e) {
var percent = parseInt(e.loaded / e.total * 100);
li.find(".progressbar").width(percent);
}, false);
// check when the upload is finished
xhr.onreadystatechange = stateChange;
// setup and send the file
xhr.open('POST', '/attachments', true);
xhr.setRequestHeader('X-FILE-NAME', file.name);
xhr.send(file);
}
我假设“progress”事件没有正确读取正确的“li”。我怀疑我必须进行某种绑定才能告诉“progress”事件使用特定变量,因为它是“li”,但我不确定我缺少什么。
I'm uploading multiple files via XmlHTTPRequest and HTML5. I have the uploading working fine, but I would like to have a progress bar for each file upload going on. The code I have, however, uses the last progress bar for ALL of the file uploads instead of each upload using its own progress bar. So this is mostly visual on the client-side, but it's really annoying me. For some reason I'm assuming that the event that attaches the progress of the file upload overwrites itself and uses the last progress bar. Here's my code:
var files = event.dataTransfer.files;
// iterate over each file to upload, send a request, and attach progress event
for (var i = 0, file; file = files[i]; i++) {
var li = $("<li>" + file.name + "<div class='progressbar'></div></li>");
// add the LI to the list of uploading files
$("#uploads").append(li);
// fade in the LI instead of just showing it
li.hide().fadeIn();
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener('progress', function(e) {
var percent = parseInt(e.loaded / e.total * 100);
li.find(".progressbar").width(percent);
}, false);
// check when the upload is finished
xhr.onreadystatechange = stateChange;
// setup and send the file
xhr.open('POST', '/attachments', true);
xhr.setRequestHeader('X-FILE-NAME', file.name);
xhr.send(file);
}
I'm assuming that the proper "li" is not getting read properly by the "progress" event. I suspect there's some sort of binding I have to do to tell the "progress" event to use a particular variable as it's "li", but I'm not sure what I'm missing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的示例无法正常工作,因为您没有考虑到在创建所有列表项时会触发 xhr Progress 事件。然而,有很多方法可以使您的示例发挥作用。这个想法是让 xhr 知道它正在处理的到底是什么列表项。例如使用这段代码(我没有检查它是否有效。这段代码的目的是描述这个想法):
Your example doesn't work properly becourse you don't take into account that xhr progress event is fired when all list items had been already created. However there are a lot of ways to make your example work. The idea is to let xhr know what exactly list item it is dealing with. For example use this code (I didn't check if it works. The purpose of this code is to describe the idea):
我想我找到了解决这个问题的办法。它看起来对我的一些测试有效。我的英语不太好,因此我只会分享我的示例脚本。如果您提出任何问题,我会尽力解释更多:)
I think I found a solution for this problem. It looks working on some of my tests. I'm not very good at english thus I'll just share my sample script. If you ask any question, I'll try to explain more :)