2013-04-16 49 views
1

我通过ajax请求上传文件,只需将它们分割成块即可。Ajax文件上传进度事件没有触发

该问题的进展情况,火狐出于某种原因想不火这种情况下,这里是我的代码(最不必要的代码被删除)

//slice file 
if(file.mozSlice){ 
    chunk = file.mozSlice(startByte, endByte); 
}else if(file.slice){ 
    chunk = file.slice(startByte, endByte); 
}else{ 
    chunk = file; 
    isLast = true; 
} 


var xhr = new XMLHttpRequest(); 

xhr.upload.addEventListener('progress', function(e){ 
    console.log('progress'); 
}, false); 

xhr.upload.addEventListener('error', function(e){ 
    console.log("upload error!"); 
}); 

xhr.onreadystatechange = function(e){ 
    if(this.readyState == 4 && this.status == 200){ 
     //this chunk has bee uploaded, proceed with the next one... 
    } 
} 

xhr.open('POST', "", true); 

xhr.setRequestHeader('Cache-Control', 'no-cache'); 
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');//header 
xhr.setRequestHeader('Content-Type', 'application/octet-stream');//generic stream header 
xhr.send(chunk); 

我敢肯定,我的天堂”由于Chrome工作没有任何问题,所以不会出现任何重大错误,所以必须有一些与Firefox相关的问题。

+1

也许它完成得这么快从未有任何进展报告? – Halcyon

+0

不,我没有尝试使用大文件,我可以看到正在上传的块,有时需要大约3秒才能上传块。 – Linas

+0

您在此处列出的代码看起来是正确的,我自己也没有在Firefox中看到任何进度事件通知问题。所以,您可能会遗漏一些导致客户端代码出现问题的问题。没有一个实例,可能很难确定问题的确切来源。 –

回答

-1

我检查了我实现我加入进度事件我打电话xhr.open后,也许能解决吗?

尝试在这里的第二个代码示例:https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/Using_XMLHttpRequest#Monitoring_progress这是否工作?

+0

那么我已经尝试过,但仍然没有。在那个例子中有一个说明:'注意:在请求调用open()之前,你需要添加事件监听器。否则,进度事件不会触发。“所以我认为我做到了正确的方式 – Linas

+0

您发布的链接演示*下载*进度,而不是*上传*进度。 –

7

xhr.upload.addEventListener('progress', function(e) { 
    console.log('progress'); 
}, false); 

火狐

xhr.addEventListener('progress', function(e) { 
    console.log('progress'); 
}, false);