前端获取下载进度

2023-12-24  本文已影响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();
          }
        }
      }
    }
    },

上一篇 下一篇

猜你喜欢

热点阅读