【问题现象】使用DWZ框架上传文件:
<form method="post" action="" class="pageForm required-validate" enctype="multipart/form-data" onsubmit="iframeCallback(this, customAjaxDone);">
<div class="pageFormContent">
<p>
<label>导入:</label>
<input name="file" class="valid" type="file">
</p>
<div class="buttonActive"><div class="buttonContent"><button type="submit">提交</button></div></div>
</div>
</form>
dwz在做无刷新文件上传时,是通过隐藏iframe去做文件提交的
后台处理请求返回JSON
@RequestMapping(value = "import/{navTabId}/{type}", method = RequestMethod.POST)
@ResponseBody
public Object importFile(@RequestParam(value = "file") MultipartFile file, @PathVariable String type, @PathVariable String navTabId,HttpServletResponse response) {
...
}
在IE浏览器会提示json文件的下载提醒
【问题解决】 由于json默认返回的响应头为“Content-Type: application/json;charset=UTF-8”导致了 IE会有这个问题
采用的解决办法就是去更改响应头
// json格式化
ObjectMapper objectMapper = new ObjectMapper();
// 设置响应头
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = null;
try {
String result = objectMapper.writeValueAsString(dwzResult);
out = response.getWriter();
out.write(result);
out.flush();
} catch (IOException e) {
logger.error("IOException", e);
} finally {
if (out != null) {
out.close();
}
}