@RequestMapping(value = "/uploadt", produces = "text/html;charset=UTF-8") @ResponseBody public String addCaseResult(HttpServletRequest request, CaseResult caseResult, @RequestParam(value = "file", required = false) MultipartFile file) { String filePath = ""; if (file != null && !file.isEmpty()) { String orName = file.getOriginalFilename(); String guid = request.getParameter("guid"); filePath = fileUpladPath + "/" + guid + "_" + orName; File outFile = new File(filePath); try { file.transferTo(outFile); } catch (Exception e) { logger.error(e.getMessage()); } } ajax.setSuccess(true); ajax.setMsg("上传成功"); ObjectMapper om = new ObjectMapper(); try { return om.writeValueAsString(ajax); } catch (Exception e) { logger.error(e.getMessage()); return ""; } }
spring 配置 <!-- 这里申明的id必须为multipartResolver --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="100000" /> </bean>
@RequestMapping(value = "/fileIsExists") @ResponseBody public AjaxJson fileIsExists( @RequestParam(value = "filePath", required = false) String filePath) { AjaxJson ajax = new AjaxJson(); if (new File(filePath).exists()) { ajax.setSuccess(true); } else { ajax.setSuccess(false); ajax.setMsg("附件不存在"); } return ajax; } /** * 下载附件 */ @RequestMapping("downloadFile") public ModelAndView downloadFile( @RequestParam(value = "filePath", required = false) String filePath, HttpServletRequest request, HttpServletResponse resp) { resp.setContentType("application/x-msdownload;"); try { request.setCharacterEncoding("UTF-8"); } catch (UnsupportedEncodingException e) { logger.error(e.getMessage()); } BufferedInputStream bis = null; BufferedOutputStream bos = null; File file = new File(filePath); if (file.exists()) { try { long fileLength = file.length(); filePath = filePath.replace("\\", "/"); String fileName = filePath.substring(filePath.lastIndexOf("/") + 1); resp.setContentType("application/x-msdownload;"); resp.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1")); resp.setHeader("Content-Length", String.valueOf(fileLength)); bis = new BufferedInputStream(new FileInputStream(filePath)); bos = new BufferedOutputStream(resp.getOutputStream()); byte[] buff = new byte[2048]; int byteRead; while (-1 != (byteRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, byteRead); } } catch (Exception e) { logger.error(e.getMessage()); } finally { closeStream(bis, bos); } } return null; } private void closeStream(BufferedInputStream bis, BufferedOutputStream bos) { try { if (bis != null) { bis.close(); } if (bos != null) { bos.close(); } } catch (IOException e) { logger.error(e.getMessage()); } }
var dataInfo = { cases: cases, description: description,password:password,insertUser:insertUser,fromUser:fromUser} ; $.ajaxFileUpload({ type : "POST", secureuri :false, url : '<%=request.getContextPath()%>/ws/caseResult/addCaseResult?guid='+guid, data : dataInfo, dataType: 'json', fileElementId: 'file', error : function(data, status, e) { alert(data.message); }, success : function(data, status) { var msg = data.msg; var success = data.success; if(success){ alert(msg); window.location.href="<%=request.getContextPath()%>/ws/caseResult/list"; }else{ alert(msg); } } });
<form name="form" id="caseResultForm" method="post" enctype="multipart/form-data"> <li class="name_width">附件:</li> <li class="reg_input"><input type="file" name="file" id="file"/></li> </form>
相关推荐
ajaxfileupload.js用于文件上传
ajaxfileupload.js, jquery异步文件上传插件
此为前端进行文件上传,使用Ajax方式提交的js插件,使用方便简洁,开发很高效。
1、jquery异步上传图片 2、js前端异步上传图片 3、下载免费 4、功能已测试可以使用
现在整理如下并附修复版的ajaxfileupload.js下载。 问题: 1:无法带参数提交,只能上传文件; 2:运行时报:jQuery.handleError is not a function 错误; 3:执行成功后,始终指向error方法处理,无法执行sucess...
NULL 博文链接:https://zhaixp1949.iteye.com/blog/2260109
折腾了一天,亲自调试完成亲测jquery+ajaxfileupload+demo
ajaxfileupload.js 异步上传jquery插件,亲测可用,有需要的可以选择下载
JSP使用ajaxFileUpload.js实现跨域问题.docx
通过修改终于使得ajaxfileupload兼容jquery3,代码已上线运行。
ajaxfileupload.js 兼容IE
博文 http://blog.csdn.net/qq_34273222/article/details/54428580 中所需要的js文件,如有问题请在博客留言。
ajax上传文件必须js文件,修复其中缺少handleError方法,json处理带有<pre></pre>,问题,亲测可以,直接引用即可!
ajaxfileupload.js 上传文件不报错版本! 放心使用!! 平时自己用的
优化过的ajaxfileupload.js文件 兼容IE,但是用的时候需要引用jquery1.8左右 不能用1.9以上版本
多上传ajaxfileupload.js