2

I have hooked up following code from different blogs and SO as I couldn't find any tutorial for doing this.

What I have to do is to download a file from the REST API using angular and typescript. I do not have the access to the api (only have the url which is like "http://..." and nothing more.

Here is my typescript code:

   //Downloading File
  // may be located in any component


  let options = {
    responseType: ResponseContentType.Blob //This has compilation error. Cannot recognize ResponseContentType.Blob
  }
  // pass options to request
  this.http.get(url, options)

let type = "my/mime";
// download the file - retrieve observable
this.http.get(url, options).map((res: Body) => new Blob([res.blob()], { type: type }));

public downloadAttachment(attachment: MediaDownloadFileDto): void {
  this.myDownloadService.downloadFile(downloadUrl, mimeType).subscribe((fileBlob: Blob) => {
      fileSaver.saveAs(fileBlob, attachment.filename);
  }, (message: string) => {
      // error handling...
  });
}

// may be located in any service:
public downloadFile(url: string, type: string): Observable<Blob> {
  let options = {
      responseType: ResponseContentType.Blob
  };
  return this.http.get(url, options).map((res: Body) => new Blob([res.blob()], { type: type }));
}

Here is the HTML Button I am using:

<button class="nui-button nui-button--alt download" (click)="downloadFile(url,options)">Download Template</button>

Thanks

4
  • Nice code. Is there a problem with it? Commented Dec 19, 2018 at 12:36
  • 1
    Yes. It's not working. Commented Dec 19, 2018 at 12:38
  • issue with http call or something else ? Commented Dec 19, 2018 at 12:46
  • @TeraPhoopa What specifically isn't working? Are there errors? If so, what are they? Commented Dec 19, 2018 at 14:22

1 Answer 1

1

You can simply use the HTML download attribute.

Try this:

<a href="https://some-rest-api.com/some-file.mp4" download>
Sign up to request clarification or add additional context in comments.

2 Comments

Consider upvoting if you found my answer useful :D
I tried upvoting it but it says my "upvote will be counted" but will not be displayed because my reputation is less than 15...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.