我正在聆听xhr.onprogress
request.onprogress = function(e){
return conf.progress ? conf.progress(e) : null;
};
其中conf.progress
是
function(e){
var position = e.position || e.loaded;
var total = e.totalSize || e.total;
var percent = ((e.loaded/e.total)*100)+"";
console.log(percent);
console.log(position, total);
console.log(e);
}
percent
在2.789069431137492e-11
这样的控制台中产生了错误的值,这就是console.log(e)
打印的内容
XMLHttpRequestProgressEvent
bubbles: false
cancelBubble: false
cancelable: true
clipboardData: undefined
currentTarget: undefined
defaultPrevented: false
eventPhase: 2
lengthComputable: false
loaded: 4982035
position: 4982035
returnValue: true
srcElement: undefined
target: undefined
timeStamp: 1323097256269
total: 18446744073709552000
totalSize: 18446744073709552000
type: "progress"
__proto__: XMLHttpRequestProgressEvent
为什么e.totalSize: 18446744073709552000
如此之大,甚至在文档完全加载后e.loaded: 4982035
totalSize
完全加载时应该等于loaded
答案 0 :(得分:0)
实际上,如果您使用的是基于WebKit的浏览器,很可能是一个WebKit错误,其长度为-1,而不检查否定:https://bugs.webkit.org/show_bug.cgi?id=36156
答案 1 :(得分:0)
那是因为totalSize is set to the maximum value of an unsigned 64-bit integer when it is unknown。您必须依靠lengthComputable
来检查是否返回了content-length
标头。