php - 在提交和uploadProgress 之前在ajax jquery 中不起作用
问题描述
谁能帮我解决这个问题,我尝试了很多方法,但是 beforeSubmit 和 uploadProgrss 不起作用
$.ajax({
url:"include/ajaxPages/admin/insertNewItem.php",
type:"POST",
data:new FormData(this),
processData:false,
contentType:false,
beforeSubmit:function(){
$(this).find(".progress").width("0%");
},
uploadProgress:function(event,position,total,complete){
$(this).find(".progress").animate({
width: complete+"%"
},{
duration:1000
})
},
success:function(data){
console.log(data);
alert("تم الاضافه بنجاح");
$(".loadingViedo").fadeOut();
window.location = document.URL;
}
})
解决方案
尝试这个:
var fd = new FormData();
fd.append('rName', $('#upload-resource .upload').attr('data-name'));
fd.append('rfile', $('#upload-resource .upload').attr('data-file'));
$.ajax({
url: 'include/ajaxPages/admin/insertNewItem.php',
data: fd,
processData: false,
contentType: false,
type: 'POST',
xhr: function () { // custom xhr
myXhr = $.ajaxSettings.xhr();
if (myXhr.upload) { // if upload property exists
myXhr.upload.addEventListener('progress', function (e) {
var done = e.position || e.loaded
var total = e.totalSize || e.total;
var percent = Math.round(done / total * 100);
//console.log('xhr progress: ' + percent + '%');
$(this).find(".progress").animate({
width: percent +"%"
},{
duration:1000
});
}, false);
myXhr.addEventListener('load', function (e) {
//upload done
});
$('#upload-resource .cancel').show().click(function () {
//cancel upload
myXhr.abort();
});
}
return myXhr;
},
success: function (data) {
//to do for success result
}
});
推荐阅读
- c# - 即使我看到来自 VSTS Web 应用程序的相关链接,C# VSTS WorkItem 相关计数字段也显示为零
- r - 特定单元格中的控制数字
- android - 在 Android Studio 中构建 APK 时会生成 Output.json。为了使应用程序正常工作,我是否需要在我的设备上复制两者?
- python - 使用 python、pytest、mock 在另一个模块中模拟第 3 方函数
- c# - Database.ExecuteSqlCommand() 挂起执行存储过程
- vba - 用于选择列定义名称的 vba 代码
- django - Django 管理员:保持安全的最佳实践
- c# - 具有完全可选参数的构造函数是不好的做法吗?
- python - numpy的ndarray中有多个元素的dtype是什么?
- netbeans - 将库添加到 Netbeans 中的 Vaadin Web App 项目