XMLHttpRequestProgressEvent.total TotalSize 给出错误值
我正在听 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
它完整的
I am listening on xhr.onprogress
request.onprogress = function(e){
return conf.progress ? conf.progress(e) : null;
};
where conf.progress
is
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
yields wrong value in console like 2.789069431137492e-11
and this is what console.log(e)
prints
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
Why the e.totalSize: 18446744073709552000
is so big and even after the document is completely loaded e.loaded: 4982035
as totalSize
should be equal to loaded
when its complete
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,如果您使用的是基于 WebKit 的浏览器,则很可能是一个 WebKit 错误,其中在不检查负数的情况下转换了 -1 的长度: https://bugs.webkit.org/show_bug.cgi?id=36156
Actually, if you're on a WebKit-based browser, it could very likely be a WebKit bug where a length of -1 is being casted without checking for a negative: https://bugs.webkit.org/show_bug.cgi?id=36156
这是因为 totalSize 设置为无符号 64 的最大值位整数未知时。您必须依靠
lengthComputable
来检查content-length
标头是否返回。That's because totalSize is set to the maximum value of an unsigned 64-bit integer when it is unknown. You gotta rely on
lengthComputable
to check whether acontent-length
header was returned or not.