spring MVC文件上传和实时进度提醒
2016-03-17 09:24
741 查看
http://www.myext.cn/extDetail/a_478.html
@RequestMapping(value = "/getProgress.do", method = RequestMethod.POST, produces = { "application/json" }) @ResponseBody public Double getProgress(HttpServletRequest request, HttpServletResponse response) { if (request.getSession().getAttribute("upload_ps") == null) { return 0.00; } ProgressEntity ps = (ProgressEntity) request.getSession().getAttribute("upload_ps"); Double percent = 0d; if (ps.getpContentLength() != 0L) { percent = (double) ps.getpBytesRead() / (double) ps.getpContentLength() * 1.0d; // 百分比 if (percent != 0d) { DecimalFormat df = new DecimalFormat("0.00"); percent = Double.parseDouble(df.format(percent)); } } return percent; } @RequestMapping(value = "/upLoadFiledir.do", method = RequestMethod.POST, produces = { "application/json" }) @ResponseBody public void upLoadFile(HttpServletRequest request, @RequestParam("file") MultipartFile[] file) { System.out.println("开始"); // 文件保存路径 String path = request.getSession().getServletContext().getRealPath("upload"); for (int i = 0; i < file.length; i++) { String fileName = file[i].getOriginalFilename(); File targetFile = new File(path, fileName); if (!targetFile.exists()) { targetFile.mkdirs(); } try { file[i].transferTo(targetFile); } catch (Exception e) { e.printStackTrace(); } } }
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> </head> <body> <script type="text/javascript"> function progress(){ $.post('<%=request.getContextPath()%>/publicdoc/getProgress.do',{},function(data){ $("#progress").attr("data-percent",data*100+"%"); $("#progress-bar").width(data*100+"%"); }); } var InterValObj; function doUpload() { layer.open({ type: 1, content: $('#progress'), area: ['400px', '200px'], title:'上传文件', end:function(){//层销毁回调函数 $("#progress").attr("data-percent","0%"); $("#progress-bar").width("0%"); }, closeBtn: 2 }); var formData = new FormData($("#form")[0]); InterValObj = window.setInterval(progress, 500); //启动计时器,1秒执行一次 $.ajax({ url: '<%=request.getContextPath()%>/publicdoc/upLoadFiledir.do' , type: 'POST', data: formData, async: true, cache: true, contentType: false, processData: false, success: function (returndata) { layer.msg("上传成功",{icon:6}); $("#progress").attr("data-percent","100%"); $("#progress-bar").width("100%"); window.clearInterval(InterValObj);//停止计时器 }, error: function (returndata) { } }); } </script> <form id="form" enctype="multipart/form-data"> 选择文件:<input type="file" name="file" multiple="multiple" id="file"> <input type="button" value="提交" onclick="doUpload()"> </form> <div class="progress" data-percent="0%" id="progress" style="width: 300px;display: none;margin: auto;margin-top: 50px"> <div class="progress-bar" style="width:0%;" id="progress-bar"></div> </div> </body> </html>
相关文章推荐
- 自动共享和上传文件到兼容的托管站点
- 实现FTP整站上传的批处理代码
- asp防止上传图片木马原理解析
- 批处理向FTP上传具有指定属性的文件(增量备份)
- 用PHP实现文件上传
- PHP实现多文件上传的方法
- PHP安全上传图片的方法
- php限制上传文件类型并保存上传文件的方法
- C#实现图片上传与浏览切换的方法
- PHP多个文件上传到服务器实例
- 通过隐藏iframe实现无刷新上传文件操作
- js验证上传图片的方法
- HTML5实现微信拍摄上传照片功能
- Webwork 实现文件上传下载代码详解
- jquery实现简洁文件上传表单样式
- jquery.blockUI.js上传滚动等待效果实现思路及代码
- uploadify多文件上传参数设置技巧
- 简单实现限制uploadify上传个数
- Uploadify上传文件方法
- C#实现附件上传和下载功能