3

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

Neel Basu
  • 12,638
  • 12
  • 82
  • 146
  • 2
    I noticed that `lengthComputable` is `false` in your case which, as I understand it, means you can't rely on `total`. I can't find a reference online for the behavior of `total` when `lengthComputable` is false -- I thought that it would be 0. David Flanagan's "JavaScript: The Definitive Guide" p508 is the only discussion I can find - all other search results for `lengthComputable` show people using it, but not discussing the behavior. – Jason Morrison Dec 28 '11 at 09:13
  • Book link: http://books.google.com/books?id=6TAODdEIxrgC&pg=PA508&lpg=PA508&dq=xhr2+lengthcomputable&source=bl&ots=ob9lDTNvXJ&sig=ir83LPAKveZzXDJOJe7-WBE0k1s&hl=en&sa=X&ei=mN36TuGDL8vMrQfPs8nqDw&ved=0CE8Q6AEwBw#v=onepage&q=xhr2%20lengthcomputable&f=false – Jason Morrison Dec 28 '11 at 09:16

2 Answers2

0

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.

Knu
  • 14,806
  • 5
  • 56
  • 89
0

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