$.ajax({
xhr: function()
{
var xhr = new window.XMLHttpRequest();
//Upload progress
xhr.upload.addEventListener("progress", function(evt){
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
//Do something with upload progress
console.log(percentComplete);
}
}, false);
//Download progress
xhr.addEventListener("progress", function(evt){
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
//Do something with download progress
console.log(percentComplete);
}
}, false);
return xhr;
},
type: 'POST',
url: "/",
data: {},
success: function(data){
//Do something success-ish
}
});
这对jQuery 1.5+不起作用,因为xhr被jqXHR(即原始XHR的高级版本)所取代。现在的问题是如何让它与jqXHR一起工作...... 有谁知道怎么做?
答案 0 :(得分:10)
试试这个
$.ajax({
url: path,
xhr: function () {
var xhr = $.ajaxSettings.xhr();
xhr.upload.onprogress = function (e) {
if (e.lengthComputable) {
console.log(e.loaded / e.total);
}
};
return xhr;
}
}).done(function (e) {
})
答案 1 :(得分:4)
我制作了一个jquery插件,以便以简单的方式处理上传或下载进度事件。使用任何jquery ajax方法,如$ .ajax,$。get,$ getJSON,$ .post等。您可以从这里下载它:https://github.com/cvelazquez/jquery.ajax.progress
var jqXHR = $.post('www.somedomain.com', {data:"real long data, o maybe a file upload"}, function(response){
/* do anything on finish */
});
$(jqXHR).on('uploading', function(proxyEvent, event){
if (event.lengthComputable) {
// You can do anything here
console.log("Uploaded " + parseInt( (event.loaded / event.total * 100), 10) + "%");
}
});
答案 2 :(得分:2)
搜索文档和其他问题,似乎有一个插件来监视jquery ajax进度:https://github.com/hiddentao/jquery.ajaxprogress