我通过ajax上传文件并在上传后处理图像.
当我尝试在firefox上通过ajax上传图像文件时,xhr进度事件在上传百分比为100%但文件上传成功时不会触发我的进度功能.
上传进度100%时谷歌浏览器触发,但Firefox没有.
Biriefly我的上传脚本:
$("#uploadbutton").click(function(){
var xhr=new XMLHttpRequest()
,fd=new FormData();
xhr.upload.addEventListener("loadstart", uploadStart, false);
xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.addEventListener("error", uploadFailed, false);
xhr.open("POST", mainurl+"ajaxupload.php");
$.each($("#upload_input").files,function(i,file){
fd.append("files_"+i,file);
});
xhr.send(fd);
});
function uploadProgress(event){
var percentComplete = Math.round(event.loaded * 100 / event.total);
console.log("pecent ",percentComplete);
}
上传启动时,uploadProgress函数runnig 1次(大部分上传时百分比为80%),但上传时没有触发fnish.
最佳答案 除了当前的处理程序之外,再添加两个:
load(如果成功完成则触发),
loadend(始终触发,始终触发).
// zero or once
xhr.addEventListener("load", uploadSuccess, false);
function uploadSuccess(event) {
console.log("Upload successful.");
}
// once
xhr.addEventListener("loadend", uploadComplete, false);
function uploadComplete(event) {
console.log("All done.");
}