简短的回答:
不,你不能这样做,你想用什么xhrFields。
龙答:
有一个XmlHttpRequest对象两份进度事件:
响应研究的进展(XmlHttpRequest.onprogress)
这是当浏览器下载从数据服务器。
请求进度(XmlHttpRequest.upload.onprogress)
这是当浏览器发送数据到服务器(包括POST参数,Cookie和文件)
在你的代码使用响应进度事件,但您需要的是请求进度事件。这是你如何做到这一点:
$.ajax({
async: true,
contentType: file.type,
data: file,
dataType: 'xml',
processData: false,
success: function(xml){
// Do stuff with the returned xml
},
type: 'post',
url: '/fileuploader/' + file.name,
xhr: function(){
// get the native XmlHttpRequest object
var xhr = $.ajaxSettings.xhr() ;
// set the onprogress event handler
xhr.upload.onprogress = function(evt){ console.log('progress', evt.loaded/evt.total*100) } ;
// set the onload event handler
xhr.upload.onload = function(){ console.log('DONE!') } ;
// return the customized object
return xhr ;
}
});
的xhr选项参数必须是返回jQuery来使用原生XMLHttpRequest对象的。