gpt4 book ai didi

javascript - Ajax 文件上传进度事件未触发

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:35:06 24 4
gpt4 key购买 nike

我通过 ajax 请求上传一个文件,只需将它们分成 block 。

问题是进度事件,出于某种原因 Firefox 不想触发该事件,这是我的代码(删除了大部分不必要的代码)

//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 相关的问题。

最佳答案

对于 Chrome:

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

对于 Firefox:

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

关于javascript - Ajax 文件上传进度事件未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16038844/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com