使用jQuery的ajaxSubmit 上传文件时,在IE中会弹出下载提示框:
页面代码:
var options = { url: "<%=path%>/upload/upload",type: "POST",dataType:'html',success:function(json) { alert("json:"+json); },error:function(er){ alert("er:"+er); //functionChange(er.responseText); } }; $('#picForm').ajaxSubmit(options);
/*** * {"fileName":"20141002125209_571slide4.jpg","path":"D:\\software\\eclipse\\workspace2\\demo_channel_terminal\\upload\\image\\20141002125209_571slide4.jpg"} * @param file * @param request * @param response * @return * @throws IOException */ @ResponseBody @RequestMapping(value = "/upload") public ModelAndView upload( @RequestParam(value = "image223",required = false) MultipartFile file,HttpServletRequest request,HttpServletResponse response) throws IOException { Map map = new HashMap(); if (ValueWidget.isNullOrEmpty(file)) { map.put("error","not specify file!!!"); } else { System.out.println("request:" + request);// org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest@7063d827 System.out.println("request:" + request.getClass().getSuperclass()); // String formFileTagName=null;//"file2" // for( ;multi.hasMoreElements();){ // String element=multi.nextElement(); // formFileTagName=element;//表单中标签的名称:file标签的名称 // // System.out.println("a:"+element+":$$"); // break; // } String fileName = file.getOriginalFilename();// 上传的文件名 fileName=fileName.replaceAll("[\\s]","");//IE中识别不了有空格的json // 保存到哪儿 String finalFileName = TimeHWUtil.formatDateByPattern(TimeHWUtil .getCurrentTimestamp(),"yyyyMMddHHmmss")+ "_" + new Random().nextInt(1000) + fileName; File savedFile = getUploadedFilePath(request,Constant2.UPLOAD_FOLDER_NAME + "/image",finalFileName,Constant2.SRC_MAIN_WEBAPP);// "D:\\software\\eclipse\\workspace2\\demo_channel_terminal\\ upload\\pic\\ys4-1.jpg" System.out.println("[upload]savedFile:" + savedFile.getAbsolutePath()); // 保存 try { file.transferTo(savedFile); } catch (Exception e) { e.printStackTrace(); } map.put("fileName",fileName); map.put("path",savedFile.getAbsolutePath()); System.out.println("map:"+map); } ModelAndView modelAndView = new ModelAndView( new MappingJacksonJsonView(),map); /* * {"fileName":"20141002125209_571slide4.jpg","path":"D:\\software\\eclipse\\workspace2\\demo_channel_terminal\\upload\\image\\20141002125209_571slide4.jpg"} * */ return modelAndView; }
解决方法:让action返回String,而不是ModelAndView.
修改后的action:
/*** * {"fileName":"20141002125209_571slide4.jpg","path":"D:\\software\\eclipse\\workspace2\\demo_channel_terminal\\upload\\image\\20141002125209_571slide4.jpg"} * @param file * @param request * @param response * @return * @throws IOException */ @ResponseBody @RequestMapping(value = "/upload") public String upload( @RequestParam(value = "image223",HttpServletResponse response) throws IOException { String content = null; Map map = new HashMap(); if (ValueWidget.isNullOrEmpty(file)) { map.put("error","not specify file!!!"); } else { System.out.println("request:" + request);// org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest@7063d827 System.out.println("request:" + request.getClass().getSuperclass()); String fileName = file.getOriginalFilename();// 上传的文件名 fileName=fileName.replaceAll("[\\s]",Constant2.SRC_MAIN_WEBAPP);// "D:\\software\\eclipse\\workspace2\\demo_channel_terminal\\ upload\\pic\\ys4-1.jpg" System.out.println("[upload]savedFile:" + savedFile.getAbsolutePath()); // 保存 try { file.transferTo(savedFile); } catch (Exception e) { e.printStackTrace(); } ObjectMapper mapper = new ObjectMapper(); map.put("fileName","a"); map.put("path",savedFile.getAbsolutePath()); try { content = mapper.writeValueAsString(map); System.out.println(content); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("map:"+map); } // ModelAndView modelAndView = new ModelAndView( // new MappingJacksonJsonView(),"path":"D:\\software\\eclipse\\workspace2\\demo_channel_terminal\\upload\\image\\20141002125209_571slide4.jpg"} * */ return content; }
项目使用spring MVC框架
参考:http://www.kankanews.com/ICkengine/archives/42240.shtml
http://www.yunmasoft.com/