繁体   English   中英

无法使用 JS 从 S3 预签名 url 下载多个文件

[英]Not able to download multiple files from S3 pre-signed urls using JS

在我开始提出这个问题之前,我已经围绕类似的问题(包括那些已经被问到但直到现在还没有回答的问题)浏览了多个 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)
}

任何帮助深表感谢。

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

但是我可以通过将 target 设置为 _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);
  document.body.appendChild(a);
  a.setAttribute('download', '');
  a.setAttribute('target', '_blank');
  a.click();
 // a.remove();
 }, 1000)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM