Ajax提交用FormData()上传文件
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;
}
}
推荐阅读