I'm using the AWS SDK to generate pre-signed download url from S3 and and I have already done that part. I can copy the generated pre-signed url into browser and the chrome will automatically download the file.
I want to achieve the same thing in Angular2, basically simulate clicking that link. So I wrote the following code:
app.component.ts
public downloadFile() {
this.downloadService.downloadFile(this.downloadLink)
.subscribe((response) => {
console.log(response);
});}
download.service.ts
public downloadFile(downloadLink: string) {
return this.http.get(downloadLink)
.map((response) => {
return response;
});
}
It indeed printed out the file content in the console but it didn't trigger the downloading. Has anybody got an idea what's the correct way to do it?
Thanks,
Here is an example to download a CSV file change the mime type for your own file.
public downloadFile(downloadLink: string) {
return this.http.get(downloadLink)
.map((response) => {
let blob = new Blob([response], { type: 'text/csv' });
let url= window.URL.createObjectURL(blob);
window.open(url);
});
}