web前端|js教程
fileUpload,进度,上传
web前端-js教程
下面我就为大家带来一篇基于fileUpload文件上传带进度条效果的实例。现在就分享给大家,也给大家做个参考。
文字页游源码,阿里云的ubuntu,蜈蚣怎么防爬虫,php调用php网页代码,域名seo之家lzw
文件上传过程中,如果我们能看到进度条会更好,实现思路是服务器端用监听器实时监听进度并存入session,客户端异步请求服务器端获得上传进度,并进行效果渲染。
短网址生成源码,vscode空格4,下载ubuntu清除整个磁盘,tomcat打包路径,sqlite多个条件查询,jquery插件 图片大小自适应,前端有mvc框架吗,网页爬虫数据采集,php 相差天数,lofter seo,html5 网站 源代码,手机端网页嵌入视频,帝国cms作文模板下载,wordpress 页面链接到分类,淘客网站用哪个开源网站管理系统,旅游网站程序哪个好lzw
效果图:
国学网站源码,vscode 查找换行符,ubuntu 常用版本,tomcat做多节点,山河app爬虫,php取日期月份,马尾seo优化多少钱,phpcms系统和网站源码关联,dz百搭手机电脑模板lzw
服务器端servlet:
public class UploadServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException { //取出监听器MyProgress在session中保存的进度信息 String progress=(String) req.getSession().getAttribute("progress"); //响应 resp.getWriter().print(progress); //清除session中保存的数据// req.getSession().removeAttribute("progress"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); DiskFileItemFactory factory=new DiskFileItemFactory(); ServletFileUpload upload=new ServletFileUpload(factory); upload.setProgressListener(new MyProgressListener(req)); try {List list = upload.parseRequest(req);for (FileItem fileItem : list) { if (fileItem.isFormField()) {//普通表单 }else{//上传文件String path=req.getRealPath("uploads");String fileName=fileItem.getName();File file=new File(path, fileName);fileItem.write(file);System.out.println("成功上传文件:"+fileName); }} } catch (Exception e) {System.out.println("文件上传发生错误!");e.printStackTrace(); } }}
服务器端监听器:
public class MyProgressListener implements ProgressListener { private HttpSession session; public MyProgressListener(HttpServletRequest request){ session = request.getSession(); } @Override public void update(long pBytesRead, long pContentLength, int pItems) { //将数据进行格式化 //已读取数据由字节转换为M double readM=pBytesRead/1024.0/1024.0; //已读取数据由字节转换为M double totalM=pContentLength/1024.0/1024.0; //已读取百分百 double percent=readM/totalM; //格式化数据 //已读取 String readf=dataFormat(pBytesRead); //总大小 String totalf=dataFormat(pContentLength); //进度百分百 NumberFormat format=NumberFormat.getPercentInstance(); String progress=format.format(percent); //将信息存入session session.setAttribute("progress", progress); //打印消息到控制台 System.out.println("pBytesRead===>"+pBytesRead); System.out.println("pContentLength==>"+pContentLength); System.out.println("pItems===>"+pItems); System.out.println("readf--->"+readf); System.out.println("totalf--->"+totalf); System.out.println("progress--->"+progress); } /** * 格式化读取数据的显示 * @param data要格式化的数据 单位byte * @return 格式化后的数据,如果小于1M显示单位为KB,如果大于1M显示单位为M */ public String dataFormat(double data){ String formdata=""; if (data>=1024*1024) {//大于等于1Mformdata=Double.toString(data/1024/1024)+"M"; }else if(data>=1024){//大于等于1KBformdata=Double.toString(data/1024)+"KB"; }else{//小于1KBformdata=Double.toString(data)+"byte"; } return formdata.substring(0, formdata.indexOf(".")+2); }}
客户端:
<base href="" rel="external nofollow" >
上传进度:0%
说明:为了让上传后该页面不跳转,我们可以让表单跳转至一个隐藏的iframe。
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
Ajax 配合node js multer 实现文件上传功能
dwz 如何去掉ajaxloading(图文教学)
Ajax返回数据之前的loading等待效果(图文教学)