今天做ajax多文件上传,前台
$.ajax({
type:'post',
url:'${ctx}/carrier/orderSale/fileupload',
data:{deviceId:id,gatherTimeBegin:asdate,gatherTimeEnd:ardate},
async: false,
dataType:'json',
success: function (data, status) {
alertify.alert("保存成功");
$('#udModal').modal('toggle');
},
error: function (data, status, e){
alertify.alert("文件上传失败");
}
});
后台
@RequestMapping(value="/fileupload")
protected void uploadAsyn(HttpServletResponse response,@RequestParam(value="fileupload",required=false)CommonsMultipartFile[] files){
。。。。
}
我采用这种形式接收,但是上传时一直报Current request is not a multipart request错误,当时在网上查找了大量案例,有说要在form上加multipart/form-data,也有说要使用post请求,但是都没有解决这个问题,后来看到了一篇文章说是因为ajax上没有用form提交,后来我把前台改成了
var form=new FormData(document.getElementById("uploadmaintenceForm"));
$.ajax({
url:"${ctx}/carrier/orderSale/fileupload",
type:"post",
data:form,
processData:false,
contentType:false,
dataType:'json',
success: function (data, status) {
alertify.alert("保存成功");
$('#udModal').modal('toggle');
},
error: function (data, status, e){
alertify.alert("文件上传失败");
}
});
<form class="form-horizontal" role="form" id="uploadmaintenceForm">
。。。
</form>
然后就可以正常上传了