1.form聲明如下 2.ajax設置如下 var formData = new FormData(document.getElementById("form")); $.ajax({ url:document.form.action, type:"POST", data:formData, pro ...
1.form聲明如下
2.ajax設置如下
var formData = new FormData(document.getElementById("form"));
$.ajax({
url:document.form.action,
type:"POST",
data:formData,
processData:false, // 告訴jquery不要處理髮送的數據
contentType:false, // 告訴jquery不要設置content-Type請求頭
success:function(data){
if(data == "ok"){
window.parent.splitpageform.submit();
}
},
error:function(data){
}
});
3.後臺controller接收方法
//將所有的照片都變成附件,這個是領用人上傳照片節點要用到的,空的也加入list
public static List<Attachment> getMultipartFilesContainEmpty(HttpServletRequest request){
List<Attachment> attachs = new ArrayList<Attachment>();
try{
//創建一個通用的多部分解析器
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//判斷 request 是否有文件上傳,即多部分請求
if(multipartResolver.isMultipart(request)){
//轉換成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
//取得request中的所有文件名
Iterator<String> iter = multiRequest.getFileNames();
while(iter.hasNext()){
//取得上傳文件
String key = iter.next();
List<MultipartFile> files = multiRequest.getFiles(key);
if(files != null && files.size() > 0 ){
for(MultipartFile file : files){
Attachment attach = FileLoad.upLoad(request, file);
if(null != attach){
attach.setTablename(key);
}else{
attach = new Attachment();
attach.setTablename("empty");
}
attachs.add(attach);
}
}
}
return attachs;
}
return attachs;
}catch(Exception e){
e.printStackTrace();
return attachs;
}
}