【发布时间】:2012-03-31 06:13:07
【问题描述】:
有谁知道为什么 upload.onprogress 在单独的功能上不能正常工作?
代码正常工作(进度条缓慢移动):
xhr.upload.onprogress = function(e) {
if (e.lengthComputable) {
progress.value = (e.loaded / e.total) * 100;
}
};
但如果我把它放到函数中,它就不再起作用了:
xhr.upload.onprogress = uploadProgress(event);
function uploadProgress(e) {
if (e.lengthComputable) {
progress.value = (e.loaded / e.total) * 100;
}
}
在第二个代码中,进度条在文件上传完成后直接跳到 100%,而不是在上传过程中很好地移动到 100%
所以,我已经尝试了提供的解决方案,如果我将函数放入其中,它确实有效。有没有办法把它放在函数之外?
function uploadFile(blobFile, fileName) {
...
...
// Listen to the upload progress for each upload.
xhr.upload.onprogress = uploadProgress;
// Progress Bar Calculation why it has to be in uploadFile function..
function uploadProgress(e) {
if (e.lengthComputable) {
progress.value = (e.loaded / e.total) * 100;
}
}
uploaders.push(xhr);
xhr.send(fd);
}
//it does not work if I put it outside the function. is there anyway to do this?
function uploadProgress(e) {
if (e.lengthComputable) {
progress.value = (e.loaded / e.total) * 100;
}
}
【问题讨论】:
标签: php ajax html file-upload xmlhttprequest