xiangpeng10
xiangpeng10

Reputation: 49

angular2 download object using pre-signed url from s3

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,

Upvotes: 0

Views: 7247

Answers (1)

Mehari
Mehari

Reputation: 3247

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) => {
               const blob = new Blob([response], { type: 'text/csv'})
               window.open(window.URL.createObjectURL(blob))
          });
       }

Upvotes: 1

Related Questions