无法使用 JS 从 S3 预签名网址下载多个文件
Not able to download multiple files from S3 pre-signed urls using JS
在我开始这个问题之前,我已经围绕类似的问题(包括那些已经被问到但直到现在还没有回答的问题)浏览了多个 Whosebug 答案。我还浏览了其中一篇中篇文章。所以,我做了相当多的研究。
我一直在尝试使用预签名 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)
}
非常感谢任何帮助。
我无法从同一浏览器选项卡上的预签名网址下载多个图像,这正是我尝试使用 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);
document.body.appendChild(a);
a.setAttribute('download', '');
a.setAttribute('target', '_blank');
a.click();
// a.remove();
}, 1000)
}
在我开始这个问题之前,我已经围绕类似的问题(包括那些已经被问到但直到现在还没有回答的问题)浏览了多个 Whosebug 答案。我还浏览了其中一篇中篇文章。所以,我做了相当多的研究。
我一直在尝试使用预签名 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)
}
非常感谢任何帮助。
我无法从同一浏览器选项卡上的预签名网址下载多个图像,这正是我尝试使用 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);
document.body.appendChild(a);
a.setAttribute('download', '');
a.setAttribute('target', '_blank');
a.click();
// a.remove();
}, 1000)
}