a标签实现批量下载,谷歌浏览器限制下载10条问题解决

核心思路:浏览器是限制瞬间下载10条,所以每下载10条让他休息会就行,用async await实现
小坑:forEach只支持同步,不支持异步,所以不能在forEach里使用async

      downFile(url){
        let a = document.createElement('a')
        console.log(this.downUrl+url)
        // window.location.href = this.downUrl+url;
        a.setAttribute('href', this.downUrl+url)
        a.setAttribute('download', url)
        document.body.appendChild(a)
        a.click()
        document.body.removeChild(a)
      },
      pause(msec) { // 异步暂停函数
          return new Promise(
              (resolve, reject) => {
                  setTimeout(resolve, msec || 1000);
              }
          );
      },
      // 批量下载弹窗
      async batchDown(){
        if(this.currentSelectList.length == 0){
          this.$tMsgbox.info({
            contentTitle: '请先选择一条数据',
            contentBody: ''
          });
          return ;
        }
        // let downFileList = [];
        let count = 0; // 已下载的文件数
        for(let file of this.currentSelectList){
          this.downFile(file.erptFilePath)
          if(++count >= 10){
            await this.pause(1000);
            count = 0;
          }
        }
      },
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现批量下载,你可以使用 OutputStream 来将下载的文件写入到本地磁盘。以下是一个简单的示例代码: ```java import java.io.FileOutputStream; import java.io.InputStream; import java.net.URL; import java.nio.file.Files; import java.nio.file.Paths; public class BatchDownloader { public static void main(String[] args) { String[] urls = { "https://example.com/file1.txt", "https://example.com/file2.txt", "https://example.com/file3.txt" }; String[] filenames = { "file1.txt", "file2.txt", "file3.txt" }; for (int i = 0; i < urls.length; i++) { try { URL url = new URL(urls[i]); InputStream inputStream = url.openStream(); FileOutputStream outputStream = new FileOutputStream(filenames[i]); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } outputStream.close(); inputStream.close(); System.out.println(filenames[i] + " downloaded successfully."); } catch (Exception e) { e.printStackTrace(); } } } } ``` 在这个示例中,我们使用一个包含文件URL的字符串数组和一个包含文件名的字符串数组来表示要下载的文件。然后,我们循环遍历这些URL,逐个下载文件并将其保存到本地磁盘中。下载过程使用 InputStream 从URL读取数据,并使用 FileOutputStream 将数据写入本地文件中。 请注意,在实际的应用中,你可能需要处理更多的异常情况、添加错误处理和日志记录等功能。此示例仅提供了一个基本的框架来帮助你理解如何使用 OutputStream 实现批量下载

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值