XHR Upload Progress is 100% from the start

I also recently had some difficulty setting an event listener for XHR onprogress events. I ended up implementing it as an anonymous function, which works beautifully:

xhr.upload.onprogress = function(evt)
{
    if (evt.lengthComputable)
    {
        var percentComplete = parseInt((evt.loaded / evt.total) * 100);
        console.log("Upload: " + percentComplete + "% complete")
    }
};

I stumbled across a lot of other gotchas along the way, though, so it’s quite likely one of those was tripping up my event listener. The only other difference between what you’ve got there and my setup is that I’m using xhr.sendAsBinary().

Leave a Comment