IE中使用ajaxSubmit上传文件弹出下载提示框

简介:

使用jQuery的ajaxSubmit 上传文件时,在IE中会弹出下载提示框:

 页面代码:

Js代码   收藏代码
  1. var options = {  
  2.                url: "<%=path%>/upload/upload",  
  3.                type: "POST",  
  4.                dataType:'html',  
  5.                success:function(json) {  
  6.                    alert("json:"+json);  
  7.  },  
  8.                error:function(er){  
  9.                    alert("er:"+er);  
  10.                    //functionChange(er.responseText);  
  11.                }  
  12.     };  
  13.         $('#picForm').ajaxSubmit(options);  

 后台代码:

Java代码   收藏代码
  1. /*** 
  2.      * {"fileName":"20141002125209_571slide4.jpg","path":"D:\\software\\eclipse\\workspace2\\demo_channel_terminal\\upload\\image\\20141002125209_571slide4.jpg"} 
  3.      * @param file 
  4.      * @param request 
  5.      * @param response 
  6.      * @return 
  7.      * @throws IOException 
  8.      */  
  9.     @ResponseBody  
  10.     @RequestMapping(value = "/upload")  
  11.     public ModelAndView upload(  
  12.             @RequestParam(value = "image223", required = false) MultipartFile file,  
  13.             HttpServletRequest request, HttpServletResponse response)  
  14.             throws IOException {  
  15.         Map map = new HashMap();  
  16.         if (ValueWidget.isNullOrEmpty(file)) {  
  17.             map.put("error""not specify file!!!");  
  18.         } else {  
  19.             System.out.println("request:" + request);// org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest@7063d827  
  20.             System.out.println("request:" + request.getClass().getSuperclass());  
  21.             // String formFileTagName=null;//"file2"  
  22.             // for( ;multi.hasMoreElements();){  
  23.             // String element=multi.nextElement();  
  24.             // formFileTagName=element;//表单中标签的名称:file标签的名称  
  25.             // // System.out.println("a:"+element+":$$");  
  26.             // break;  
  27.             // }  
  28.             String fileName = file.getOriginalFilename();// 上传的文件名  
  29.             fileName=fileName.replaceAll("[\\s]",   "");//IE中识别不了有空格的json  
  30.             // 保存到哪儿  
  31.             String finalFileName = TimeHWUtil.formatDateByPattern(TimeHWUtil  
  32.                     .getCurrentTimestamp(),"yyyyMMddHHmmss")+ "_"  
  33.                             + new Random().nextInt(1000) + fileName;  
  34.             File savedFile = getUploadedFilePath(request,  
  35.                     Constant2.UPLOAD_FOLDER_NAME + "/image", finalFileName,  
  36.                     Constant2.SRC_MAIN_WEBAPP);// "D:\\software\\eclipse\\workspace2\\demo_channel_terminal\\ upload\\pic\\ys4-1.jpg"  
  37.             System.out.println("[upload]savedFile:"  
  38.                     + savedFile.getAbsolutePath());  
  39.             // 保存  
  40.             try {  
  41.                 file.transferTo(savedFile);  
  42.             } catch (Exception e) {  
  43.                 e.printStackTrace();  
  44.             }  
  45.               
  46.             map.put("fileName", fileName);  
  47.             map.put("path", savedFile.getAbsolutePath());  
  48.             System.out.println("map:"+map);  
  49.         }  
  50.         ModelAndView modelAndView = new ModelAndView(  
  51.                 new MappingJacksonJsonView(), map);  
  52. /* 
  53.  * {"fileName":"20141002125209_571slide4.jpg","path":"D:\\software\\eclipse\\workspace2\\demo_channel_terminal\\upload\\image\\20141002125209_571slide4.jpg"} 
  54.  * */  
  55.         return modelAndView;  
  56.   
  57.     }  

 解决方法:让action返回String,而不是ModelAndView.

修改后的action:

Java代码   收藏代码
  1. /*** 
  2.      * {"fileName":"20141002125209_571slide4.jpg","path":"D:\\software\\eclipse\\workspace2\\demo_channel_terminal\\upload\\image\\20141002125209_571slide4.jpg"} 
  3.      * @param file 
  4.      * @param request 
  5.      * @param response 
  6.      * @return 
  7.      * @throws IOException 
  8.      */  
  9.     @ResponseBody  
  10.     @RequestMapping(value = "/upload")  
  11.     public String upload(  
  12.             @RequestParam(value = "image223", required = false) MultipartFile file,  
  13.             HttpServletRequest request, HttpServletResponse response)  
  14.             throws IOException {  
  15.         String content = null;  
  16.         Map map = new HashMap();  
  17.         if (ValueWidget.isNullOrEmpty(file)) {  
  18.             map.put("error""not specify file!!!");  
  19.         } else {  
  20.             System.out.println("request:" + request);// org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest@7063d827  
  21.             System.out.println("request:" + request.getClass().getSuperclass());  
  22.               
  23.               
  24.             String fileName = file.getOriginalFilename();// 上传的文件名  
  25.             fileName=fileName.replaceAll("[\\s]",   "");//IE中识别不了有空格的json  
  26.             // 保存到哪儿  
  27.             String finalFileName = TimeHWUtil.formatDateByPattern(TimeHWUtil  
  28.                     .getCurrentTimestamp(),"yyyyMMddHHmmss")+ "_"  
  29.                             + new Random().nextInt(1000) + fileName;  
  30.             File savedFile = getUploadedFilePath(request,  
  31.                     Constant2.UPLOAD_FOLDER_NAME + "/image", finalFileName,  
  32.                     Constant2.SRC_MAIN_WEBAPP);// "D:\\software\\eclipse\\workspace2\\demo_channel_terminal\\ upload\\pic\\ys4-1.jpg"  
  33.             System.out.println("[upload]savedFile:"  
  34.                     + savedFile.getAbsolutePath());  
  35.             // 保存  
  36.             try {  
  37.                 file.transferTo(savedFile);  
  38.             } catch (Exception e) {  
  39.                 e.printStackTrace();  
  40.             }  
  41.           
  42.             ObjectMapper mapper = new ObjectMapper();  
  43.               
  44.               
  45.   
  46.             map.put("fileName""a");  
  47.             map.put("path", savedFile.getAbsolutePath());  
  48.             try {  
  49.                 content = mapper.writeValueAsString(map);  
  50.                 System.out.println(content);  
  51.             } catch (JsonGenerationException e) {  
  52.                 e.printStackTrace();  
  53.             } catch (JsonMappingException e) {  
  54.                 e.printStackTrace();  
  55.             } catch (IOException e) {  
  56.                 e.printStackTrace();  
  57.             }  
  58.             System.out.println("map:"+map);  
  59.         }  
  60. //      ModelAndView modelAndView = new ModelAndView(  
  61. //              new MappingJacksonJsonView(), map);  
  62. /* 
  63.  * {"fileName":"20141002125209_571slide4.jpg","path":"D:\\software\\eclipse\\workspace2\\demo_channel_terminal\\upload\\image\\20141002125209_571slide4.jpg"} 
  64.  * */  
  65.         return content;  
  66.   
  67.     }  

 

项目使用spring MVC框架

参考:http://www.kankanews.com/ICkengine/archives/42240.shtml

http://www.yunmasoft.com/

相关文章
|
7月前
|
Web App开发 XML 编解码
IE浏览器下载文件中文文件名乱码问题解决
IE浏览器下载文件中文文件名乱码问题解决
84 0
|
JavaScript
ie8 ie浏览器下载excel文件乱码,变成压缩包,解决方案
ie8 ie浏览器下载excel文件乱码,变成压缩包,解决方案
172 0
|
安全 测试技术 Windows