【问题标题】:Not able to download multiple files from S3 pre-signed urls using JS无法使用 JS 从 S3 预签名网址下载多个文件
【发布时间】:2022-11-23 23:35:08
【问题描述】:

在我开始这个问题之前,我已经围绕类似的问题(包括那些已经被问到但直到现在还没有回答的问题)浏览了多个 stackoverflow 答案。我还浏览了其中一篇中篇文章。所以,我做了相当多的研究。

我一直在尝试使用预签名的 url 下载多个文件。下面的代码几天前就可以使用(这听起来很熟悉;))但是目前,我只能下载一个文件,而且下载也是随机的。有时会下载第一个文件,有时会下载最后一个。代码如下:

downloadItem() {
  let urls = [];
  for(let item of this.selectedRowsData) {
    //calling the service to fetch the presigned url
    this.dataService.getPresignedToDownloadAll(
      item.value,
      item.id).subscribe((res) => {
        urls.push(res);
        this.download(urls);
        /**if(urls.length === selectedRowData.length) {
           this.download(urls);
        }**/ //have tried this code too where I just invoke download only once when I have all the presigned urls
    });
 }
}
download(urls: any) {
  var self = this;
  var url = urls.pop();
  setTimeout(function(){
    self.snackBarService.loadComponent({
      isSuccess: true,
      message: MESSAGES.downloadInProgress,
    });
  var a = document.createElement('a');
  a.setAttribute('href', url);
  document.body.appendChild(a);
  a.setAttribute('download', '');
  a.setAttribute('target', '_self');
  a.click();
  // a.remove();
  }, 1000)
}

任何帮助深表感谢。

【问题讨论】:

    标签: javascript angular google-chrome amazon-s3


    【解决方案1】:

    我无法从同一浏览器选项卡上的预签名 url 下载多个图像,这正是我尝试使用 a.setAttribute('target', '_self'); 做的

    但是我能够通过将目标设置为 _blank 来使其工作,这确实会打开一个新选项卡,但在下载完成后会关闭那些打开的选项卡。虽然这不是一个很好的用户体验,但截至目前,我们已经继续实施了这一实施。这就是最终代码的样子

    downloadItem() {
      let urls = [];
      for(let item of this.selectedRowsData) {
      //calling the service to fetch the presigned url
      this.dataService.getPresignedToDownloadAll(
        item.value,
        item.id).subscribe((res) => {
          urls.push(res);
          this.download(urls);
       });
     }
    }
    
    download(urls: any) {
      var self = this;
      var url = urls.pop();
      setTimeout(function(){
        self.snackBarService.loadComponent({
          isSuccess: true,
          message: MESSAGES.downloadInProgress,
        });
      var a = document.createElement('a');
      a.setAttribute('href', url);
      a.setAttribute('download', '');
      a.setAttribute('target', '_blank');
      document.body.appendChild(a);
      a.click();
     // a.remove();
     }, 1000)
    }
    

    【讨论】:

      猜你喜欢
      • 2016-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-29
      • 1970-01-01
      • 2017-01-13
      • 2020-11-16
      • 2018-05-21
      相关资源
      最近更新 更多