Ajax提交表单的文件作为参数
1 $(function () { 2 $("#btnSave2").on("click", function () { 3 var url = "ReportWeekEditHandler.ashx"; 4 var data = $("form").serialize();//普通表单传参 5 6 7 //表单的文件作为参数 8 9 //方式一,单独增加传参 10 //var formData = new FormData(); 11 //formData.append("input", $("#dateYear").find("option:selected").val()); 12 //formData.append("input", $("#dateWeek").find("option:selected").val()); 13 //formData.append("input", $("#workContent").val()); 14 //formData.append("file", $("#upload_view")[0].files[0]); 15 //formData.append("file2", $("#upload_img")[0].files[0]); 16 17 18 //方式二,将form格式化FormData 19 var formData = new FormData(document.querySelector("form")); 20 21 try { 22 23 $.ajax({ 24 url: url, 25 type: 'POST', 26 data: formData, 27 // 告诉jQuery不要去处理发送的数据 28 processData: false, 29 // 告诉jQuery不要去设置Content-Type请求头 30 contentType: false, 31 beforeSend: function () { 32 console.log("正在进行,请稍候"); 33 }, 34 success: function (responseStr) { 35 if (responseStr.status === 0) { 36 console.log("成功" + responseStr); 37 } else { 38 console.log("失败"); 39 } 40 }, 41 error: function (responseStr) { 42 console.log("error"); 43 } 44 }); 45 46 } catch (e) { 47 alert(e); 48 } 49 50 return false; 51 }); 52 });
参考资料: https://blog.csdn.net/qq_34720759/article/details/78885657
如有侵权请告知,及时删除。