[英]Download multiple files using S3 pre-signed url in my Angular Application
I am making REST API call to get an array of pre-signed URLs from S3.我正在调用 REST API 以从 S3 获取一组预签名的 URL。 These URL are files that could be XML, CSV, JSON etc.这些 URL 是可以是 XML、CSV、JSON 等的文件。
How do I loop download files from these URLs without opening a new tab?如何在不打开新标签的情况下从这些 URL 循环下载文件? I do not want to use AWS SDK for NodeJS to avoid tight coupling with my front-end.我不想使用 AWS SDK for NodeJS 来避免与我的前端紧密耦合。 Application currently has Angular 7, NodeJS and ExpressJS.应用程序目前有 Angular 7、NodeJS 和 ExpressJS。
getFile(url, params){
this.awsservice.getFile(url, params).subscribe(
(response) => {
const res = JSON.parse(JSON.stringify(response));
var apiList = [];
for (var key in res) {
if(key == 'api'){
apiList = res[key];
}
}
apiList.forEach(function (url) {
// Logic to download file
document.location.assign(url) //Only seems to download the last file in the array
console.log("Download started: "+url);
});
},
(error) => {
this.tempErrorFlag = true;
const errorMsg = error;
console.log(`ERROR ::: reInitiate API ::: ${errorMsg.message}`);
});
}
I tried adding document.location.assign(url)
but it only seems to download the last url in the array.我尝试添加document.location.assign(url)
但它似乎只下载了数组中的最后一个 url。 Even adding delay
didn't help.即使增加delay
也无济于事。
Appreciate your help.感谢你的帮助。
I was able to download the files but not in the same tab.我能够下载文件,但不能在同一个选项卡中。 First I fetch presigned URLs from my angular service, which I feed into an array.首先,我从我的 Angular 服务中获取预签名的 URL,并将其输入到一个数组中。 Then basically with each of the presigned urls, I create a temporary anchor tag to and assign href the presigned url value.然后基本上使用每个预签名的 url,我创建一个临时锚标记并分配 href 预签名的 url 值。 The snackbarService is for showing a popup when the download begins. snapbarService 用于在下载开始时显示一个弹出窗口。 The entire code looks like below:整个代码如下所示:
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.