檔案和物件同時提交組裝formData
1.提交組裝資料
var file1 = document.getElementById("imgSdf1").files[0]; var file2 = document.getElementById("imgSdf2").files[0]; var file3 = document.getElementById("imgSdf3").files[0];
var formData = new FormData(); formData.append("imgSdf1", file1); //加入檔案物件 formData.append("imgSdf2", file2); //加入檔案物件 formData.append("imgSdf3", file3); //加入檔案物件 formData.append("pd", decodeURIComponent(formSearch,true)); //加入檔案物件,解碼,中文亂碼問題
$.ajax({ type: "POST", url: "${path}/people/savePeople", data: formData, processData: false,// 不加會報錯 contentType: false,// 不加會報錯 success: function (result) {
}
});
2.後臺程式碼
//檔案和資料一同上傳,需要如下操作 // public Object savePeople(@RequestParam(value = "imgSdf1", required = false) MultipartFile file, // @RequestParam(value = "imgSdf2", required = false) MultipartFile file2, // @RequestParam(value = "imgSdf3", required = false) MultipartFile file3,HttpServletRequest request) {
}