3

我在听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它完成时

4

2 回答 2

0

实际上,如果您使用的是基于 WebKit 的浏览器,它很可能是一个 WebKit 错误,其中 -1 的长度被强制转换而没有检查是否有负数:https://bugs.webkit.org/show_bug.cgi?编号=36156

于 2011-12-28T11:42:40.063 回答
0

这是因为totalSize 在未知时被设置为无符号 64 位整数的最大值。您必须依靠lengthComputable检查是否content-length返回了标头。

于 2018-07-28T20:56:47.820 回答