admin管理员组

文章数量:1427327

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
}

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
}
Share Improve this question edited Sep 14, 2011 at 0:20 Kenneth Spencer asked Sep 14, 2011 at 0:15 Kenneth SpencerKenneth Spencer 1,55213 silver badges15 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 7

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/2006/webapi/XMLHttpRequest-2/#differences

本文标签: javascriptHow to get progress when uploading file VIA XMLHttpRequestStack Overflow