文件下载进度监听

原文链接     https://segmentfault.com/q/1010000011113418

 

js 怎么判断文件或图片下载的进度?

 

简单的示例:

xhr.loaded表示已经下载多少
xhr.total表示总共多少

做个除法即可

(下面示例仅举例如何监听progress)





    
    
    
    Document






JQUERY方案(Updated version for jQuery > 1.5.1

$.ajax({
  xhr: function()
  {
    var xhr = new window.XMLHttpRequest();
    //Upload progress
    xhr.upload.addEventListener("progress", function(evt){
      if (evt.lengthComputable) {
        var percentComplete = evt.loaded / evt.total;
        //Do something with upload progress (上传中干点事)
        console.log(percentComplete);
      }
    }, false);
    //Download progress
    xhr.addEventListener("progress", function(evt){
      if (evt.lengthComputable) {
        var percentComplete = evt.loaded / evt.total;
        //Do something with download progress(下载中中干点事)
        console.log(percentComplete);
      }
    }, false);
    return xhr;
  },
  type: 'POST',
  url: "/",
  data: {},
  success: function(data){
    //Do something success-ish
  }
});

http://www.dave-bond.com/blog...

你可能感兴趣的:(jquery,JS,图片上传)