admin 管理员组

文章数量: 1184232

批量下载时,如果文件超过10个,谷歌浏览器只会下载前10个。【谷歌浏览器的一种压力保护吧】

可以使用以下方式解决这个问题。

   fileList:[];//要下载的文件      
   downFile(url){
        let a = document.createElement('a')
        console.log(this.downUrl+url)
        a.setAttribute('href', this.downUrl+url)
        a.setAttribute('download', url)
        document.body.appendChild(a)
        a.click()
        document.body.removeChild(a)
      },
   
      // 核心,一次下载超过10个文件时,执行异步暂停函数,暂停一会儿之后继续下载
      pause(msec) {
          return new Promise(
              (resolve, reject) => {
                  setTimeout(resolve, msec || 1000);
              }
          );
      },
      // 批量下载弹窗
      async batchDown(){
        let count = 0; // 已下载的文件数
        for(let file of this.fileList){
          this.downFile(file.url)
          if(++count >= 10){
            await this.pause(1000);
            count = 0;
          }
        }
      },

本文标签: 批量 单词 浏览器