XMLHttpRequestProgressEvent.total totalSize给出错误的值

时间:2011-12-05 15:18:07

标签: ajax file-upload xmlhttprequest ajax-upload

我正在聆听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);
}

percent2.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

2 个答案:

答案 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标头。