优秀的程序员从不墨迹,直接上示例代码:
前端代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div>
<input id="upload_file_id" type="file" name="files">
<br>
<input type="button" value="提交" onclick="upload()">
</div>
<script type="text/javascript">
function upload() {
var formData = new FormData();
formData.append("file1",document.getElementById('upload_file_id').files[0]);
formData.append("useSass", "true");//携带一个参数
$.ajax({
url: 'xxx/xxx/xxx',
type: 'POST',
data: formData, // formdata封装的数据包
dataType: 'JSON',
cache: false, // 不缓存
processData: false, // jQuery不要去处理发送的数据
contentType: false, // jQuery不要去设置Content-Type请求头
success: function(data) {
alert("success");
}
});
}
</script>
</body>
</html>
Java代码:
@RequestMapping(value = "/upload/multiple", method = RequestMethod.POST)
@ResponseBody
public Result ticketMultiple(HttpServletRequest request, @RequestParam("file1") MultipartFile[] files,@RequestParam(value = "useSass") String useSass) throws Exception {
}