XMLHttpRequestProgressEvent.total TotalSize 给出错误值

发布于 2024-12-19 09:29:30 字数 1250 浏览 0 评论 0原文

我正在听 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美羊羊 2024-12-26 09:29:30

实际上,如果您使用的是基于 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

不醒的梦 2024-12-26 09:29:30

这是因为 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 a content-length header was returned or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文