ajax上传文件
ajax上传文件
解决方法:
1.前端代码
<div style="margin-left: 20%;margin-top: 10%;">
<form id="uploadFile" >
<input type="file" name="files" multiple="multiple" />
<input type="hidden" name="projectId" value="1234"/>
</form>
</div>
2.ajax代码:
<script>
$("input[type='file']").on("change",function(){
var formData = new FormData($( "#uploadFile" )[0]);
$.ajax({
type: "POST",
contentType: false,
processData: false,
url:"/upload/uploadFile", //url
data:formData,
async: false,
error: function(request) {
alert("出错了");
},
success: function(data) {
}
});
})
</script>
3.后台代码:
@ResponseBody
@RequestMapping(value="/uploadFile",method = RequestMethod.POST)
public String uploadFile(@RequestParam("files") MultipartFile[] files,String projectId) {
return "null";
}