业务需要用ajax异步上传excel文件并回传解析后的错误信息,后台spring mvc, jquery 2.1.3, ajaxfileupload.js
一 页面js代码:
$("#importUserButton").click(function(){ alert("here1"); $.ajaxFileUpload({ url:serverUri+"/user/importuser", type: "POST", cache : false, secureuri:false, fileElementId:"importUserFile", dataType: "json", success : function(data) { alert(data.success); }, error : function(data) { alert("error"); } }); });二 后台代码:
IE10下返回json串会提示下载,所以ContentType设置为‘text’
@ResponseBody @RequestMapping(value = "/importuser", method = RequestMethod.POST) public Map<String,Object> importUser(HttpServletRequest request, HttpServletResponse response) { response.setContentType("text/html;charset=UTF-8"); MultipartHttpServletRequest mulRequest = (MultipartHttpServletRequest) request; MultipartFile file = mulRequest.getFile("importUserFile"); String fileName = file.getOriginalFilename(); System.out.println("fileName:"+fileName); Workbook wb = null; try { InputStream in = file.getInputStream(); if (fileName.endsWith(".xls") || fileName.endsWith(".XLS")) { wb = new HSSFWorkbook(in); } else if (fileName.endsWith(".xlsx") || fileName.endsWith(".XLSX")) { wb = new XSSFWorkbook(in); } in.close(); } catch (IOException e) { e.printStackTrace(); } .......... .......... Map<String, Object> map = new HashMap<String, Object>(); map.put("success", "true"); return map; }三 ajaxfileupload.js 遇到过以下问题:
1.handleError 无法找到
2.上传后只执行error 函数
3.不支持IE(IE 下上传无反应)
附上改后全能的js文件。