XHR Upload Progress is 100% from the start

前端 未结 3 702
温柔的废话
温柔的废话 2021-02-05 03:57

I\'m trying out the new XMLHTTPRequestUpload feature to upload some files to a php script, it mostly works fine, the upload starts, I get the finish response etc - but the progr

3条回答
  •  迷失自我
    2021-02-05 04:34

    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().

提交回复
热议问题