美文网首页
前端获取下载进度

前端获取下载进度

作者: 一个健康马 | 来源:发表于2023-12-24 14:54 被阅读0次

 getPercent(url){
      let xhr = new XMLHttpRequest();
      // xhr.abort()取消请求
      xhr.open('GET', url, true);
      xhr.onprogress = function (e) {
        let percent = Math.floor( e.loaded / e.total * 100); // 下载进度
        if(percent==100){
          window.location.href = `ngie://${that.directSeedingId}`;
        }
        console.log(percent, '进度');
      };
      xhr.send();
      xhr.responseType = "arraybuffer";
      xhr.onreadystatechange = event => {
        if(xhr.readyState == 4){
          if (xhr.status == 200){
            const fileName = 'metaWorks.exe';
            let blob = new Blob([xhr.response], { type: "exe", application: "application/octet-stream" }); // 文件类型
            const downLoadLink = document.createElement('a');
            downLoadLink.download = fileName;
            downLoadLink.href = URL.createObjectURL(blob);
            downLoadLink.click();
          }
        }
      }
    }
    },

相关文章

网友评论

      本文标题:前端获取下载进度

      本文链接:https://www.haomeiwen.com/subject/rdlhndtx.html