有人知道为什么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;
}
}
解决方法:
使用uploadProgress(事件);您调用函数本身并将返回值赋给xhr.upload.onprogress,而不是将其指定为回调函数:
xhr.upload.onprogress = uploadProgress;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。