HTML代码(目前不需要form标签的方式):
JavaScript代码:
function uploadFile(fileType){//文件上传
var fileObj = document.getElementById(fileType).files[0]; // js 获取文件对象
var formFile = new FormData();
formFile.append("file", fileObj); //加入文件对象
var data = formFile;
$.ajax({
type : "post",
url : "${ctx}/record/file/upload/" + fileType,
data : data,
cache : false,
async : true,
processData : false,//不处理发送的数据
contentType : false,//不设置请求头
success : function(){
console.log("success");
},
error : function(){
console.log("err");
}
});
}
Java后台代码:
@RequestMapping(value = "upload/{whatFile}")
public String upload(@PathVariable("whatFile") String whatFile, Model model,
@RequestParam("file") MultipartFile file,
HttpServletRequest request, HttpServletResponse response) throws IOException {
//文件上传...
return "";
}