使用 H5的 FormData对象,提交表单数据,包含文件,其它的数据都能拿到,只有文件对象拿不到
前台代码
var form = new FormData();//创建表单对象
form.append("communityParenty.name", name);
form.append("communityParenty.phone", phone);
form.append("communityParenty.startDate", startDate);
form.append("communityParenty.overDate", endDate);
form.append("communityParenty.status", status);
form.append("communityParenty.description", discription);
if(headImg != null) {
form.append("headImg.fileName", headImg.name);
form.append("headImg.fileSize", headImg.size);
form.append("headImg.fileType", headImg.type);
form.append("headImg.file", headImg);
}
var url = "/communityParenty/addCommunityParenty";
$.ajax({
type: "POST",
url: url,
enctype: "multipart/form-data",
contentType: false,
processData: false,
cache: false,
data: form,
dataType: "json",
xhr: xhrOnProgress(function(e) {
var percent=e.loaded / e.total;//计算百分比
console.log("ajax发送数据百分比:" + percent);
}),
success: function(jsonData) {
var data = $.parseJSON(jsonData);
alert(jsonData);
},
error: function() {
alert("请求失败");
}
});
}
后台代码
@Ok("json")
@AdaptBy(type = UploadAdaptor.class)
public Object addCommunityParenty(HttpServletRequest request, HttpServletResponse response,
@Param("::communityParenty.") UserCommunityParenty userCommunityParenty,
@Param("::headImg.") FileEntity headImg) throws IOException, ServletException {
//FileEntity是一个 文件对象,里面包含了一个 File对象
}