$.ajax({
url: url,
type: 'POST',
data: formData,
cache: false,
contentType: false,
processData: false,
dataType: "json",
xhr: function () {
//获取ajax中的ajaxSettings的xhr对象,为upload属性绑定progress事件的处理函数
var xhr = $.ajaxSettings.xhr();
if (xhr.upload) {
//检查其属性upload是否存在
xhr.upload.addEventListener("progress", function (e) {
if (!e.lengthComputable) {
console.log("不支持")
return;
}
//计算进度
var percent = (e.loaded / e.total * 100).toFixed(0);
$("#uploadPercent" ).text(percent + "%");
$("#progressPercent").css("width", percent + "%");
if(parseInt(percent) == 100) {
//隐藏进度
$("#uploadProgressModal").modal("hide");
commons.loadingShow();
}
}, false);
}
return xhr;
},
success: function (data) {
console.log(data);
},
error: function (request, status, error) {
console.log(error);
}
});