How to get progress when uploading file VIA XMLHttpRequest

被刻印的时光 ゝ 提交于 2019-12-01 08:20:43

问题


I am wondering how to get the progress of a file upload using XMLHTTPRequest. In Firefox the onprogress method does not fire at all, and in chrome it only fires after the file has finished uploading.

function fileUpload(file)
{
    var formData = new FormData();
    formData.append('file', file);

    var xhr = new XMLHttpRequest();
    xhr.onprogress = function(e)
    {
        alert('progress');
    };

    xhr.open('POST', 'post.php', true);

    xhr.send(formData);  // multipart/form-data
}

回答1:


Try xhr.upload.onprogress. In the XMLHttpRequest2 spec XMLHttpRequest have upload attribute.

The ability to register for progress events. Both for downloads (put listeners on the XMLHttpRequest object itself) and uploads (put listeners on the XMLHttpRequestUpload object, returned by the upload attribute). http://dev.w3.org/2006/webapi/XMLHttpRequest-2/#differences



来源:https://stackoverflow.com/questions/7409954/how-to-get-progress-when-uploading-file-via-xmlhttprequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!