我是靠谱客的博主 烂漫奇迹,最近开发中收集的这篇文章主要介绍bootstrap 文件上传进度条,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

div:

    <div class="modal fade" id="processModal" data-keyboard="false"  data-backdrop="static" tabindex="-1" role="dialog"  aria-labelledby="processModalLabel" aria-hidden="true" style="font-family: 微软雅黑;">
  <div class="modal-dialog">
   <div class="modal-content">
    <div class="modal-header">
     <h4 class="modal-title modalTitleText" id="processModalLabel">文件上传进度</h4>
    </div>
    <div class="modal-body">
                         <div class="progress">
                                <div class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 0%;"></div>
                        </div>         
    </div>
   </div>
  </div>
 </div>

 js:

function run(){
  $.ajax
        ({ //请求登录处理页
            url: ContextPath + '/File/process.action',
            //传送请求数据
            dateType : "text",
            type:"POST",
            success: function (date) { //登录成功后返回的数据
                //根据返回值进行状态显示  
              if(date<100){
               $("div[class=progress-bar]").css("width",date+"%").text(date+"%");           
                     setTimeout("run()",500);
                 }else{
                  $("div[class=progress-bar]").css("width",date+"%").text(date+"%");
                 }                      
           
            }
        })
   }

 

java: springMvc
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

 

 public class CustomMultipartResolver extends CommonsMultipartResolver {

  private HttpServletRequest request;
  @Override
  protected  FileUpload newFileUpload(FileItemFactory fileItemFactory)
  {
  ServletFileUpload upload = new ServletFileUpload(fileItemFactory);
  upload.setSizeMax(-1);
  if(request!=null)
  {
  ///System.out.println("注入监听");
  FileUploadListener uploadProgressListener = new FileUploadListener();
  upload.setProgressListener(uploadProgressListener);
  HttpSession session = request.getSession();
  session.setAttribute("uploadProgressListener", uploadProgressListener);
  }
  return upload;
  }
  @Override
  public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException {
         this.request=request;//获取到request,要用到session
         return super.resolveMultipart(request);
   }
  }

 

import org.apache.commons.fileupload.ProgressListener;

public class FileUploadListener implements ProgressListener {
 
 private long num100Ks = 0;
 private long theBytesRead = 0;
 private long theContentLength = -1;
 private int whichItem = 0;
 private int percentDone = 0;
 private boolean contentLengthKnown = false;

 public void update(long bytesRead, long contentLength, int items) {

 if (contentLength > -1) {
 contentLengthKnown = true;
 }
 theBytesRead = bytesRead;
 theContentLength = contentLength;
 whichItem = items;

 long nowNum100Ks = bytesRead / 100000;
 if (nowNum100Ks > num100Ks) {
 num100Ks = nowNum100Ks;
 if (contentLengthKnown) {
 percentDone = (int) Math.round(100.00 * bytesRead / contentLength);
 }

 }
 }

 public int getPercentDone() {
 return percentDone;
 }
}

 

最后

以上就是烂漫奇迹为你收集整理的bootstrap 文件上传进度条的全部内容,希望文章能够帮你解决bootstrap 文件上传进度条所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(35)

评论列表共有 0 条评论

立即
投稿
返回
顶部