作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我通过 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/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!