如何在上传文件VIA XMLHttpRequest时取得进展 [英] How to get progress when uploading file VIA XMLHttpRequest
本文介绍了如何在上传文件VIA XMLHttpRequest时取得进展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何使用XMLHTTPRequest获取文件上传的进度。在Firefox中,onprogress方法根本不会触发,而在chrome中,它只会在文件上传完成后触发。
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
}
推荐答案
尝试 xhr.upload.onprogress
。在XMLHttpRequest2规范中,XMLHttpRequest具有upload属性。
Try xhr.upload.onprogress
. In the XMLHttpRequest2 spec XMLHttpRequest have upload attribute.
注册进度事件的能力。下载(在XMLHttpRequest对象本身上放置
侦听器)和上传(在XMLHttpRequestUpload对象上放置
侦听器,由上传
属性返回)。
http://dev.w3.org/2006/webapi/ XMLHttpRequest-2 /#差异
这篇关于如何在上传文件VIA XMLHttpRequest时取得进展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文