SpringMVC+ajaxFileUpload上传图片 IE浏览器弹下载框问题解决方案

来源:互联网 发布:var(x|y) 算法 编辑:程序博客网 时间:2024/06/08 09:33

来源:小灯光环

http://www.2cto.com/kf/201506/404534.html


如题,简单记录一下这个问题的解决办法,导致问题的核心原因是:ajaxfileupload不支持响应头ContentType为application/json的设置,并且IE也不支持这种格式,而当我们用SpringMVC的@ResponseBody注解的时候会自动将响应类型设置为application/json,所以解决办法只有手动设置响应类型。

Step1 弃用@ResponseBody,通过流手动输出响应,并指定响应类型为text/html


@SuppressWarnings("unchecked")@RequestMapping(value = "/importData", method = RequestMethod.POST)@Log(name = "导入用户", model = "用户管理", log = "导入用户")public Map<String,Object> importData(@RequestParam("myfile") CommonsMultipartFile myfile,HttpServletResponse response) throws IOException {//逻辑业务Map<String, Object> map = ExcelTool.checkExcel(myfile, titlee);//改用response输出响应数据response.setContentType("text/html;charset=utf-8");response.getWriter().write(mapper.toJson(map));return null;}

js代码

$.ajaxFileUpload({url: "../user/importData",type: 'post',secureuri: false, //是否启用安全提交,默认为falsefileElementId: 'myfile', //文件选择框的id属性contentType: "text/html; charset=utf-8",dataType: 'text',success: function(data, status) {alert(data)},error: function(data, status, e) {alert('upload Failed!');}});


0 0