1、html部分,引入jquery-form.js插件采用表单上传方式
<script type="text/javascript" src="/js/jquery-form.js"></script>
<div>
<form id="uploadForm" enctype="multipart/form-data" method="post">
<input id="file" type="file" name="file" />
</form>
<button id="upload">上传文件</button>
</div>
2、js部分,需设置返回类型dataType:'html',
$("#upload").on("click", function() {
$("#uploadForm").ajaxSubmit({
url : "${uploadUrl}",
type: "POST",
dataType:'html',
success : function(data) {
alert(data);
},
error: function() {
alert("上传失败,请重新上传!");
}
});
});
3、controller部分
需指定响应体返回类型和编码
@PostMapping(value=MODEL + "/excel/import.do",produces="text/html;UTF-8")
@ResponseBody
public String importExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request)
throws Exception {
if (file.isEmpty()) {
return "文件为空!";
}
return "success";
}
前台后台指定好响应类型即可在ie8下正常提示