[Fixed] How do I save a blob response type in Angular 4+


So I am calling an API, which returns to my Angular a file, like so:

getFile(fileInformation) {
  const req = new HttpRequest('POST', 'filedownload', fileInformation, { reportProgress: true, responseType: "blob" });
  return this.http.request(req);

And in the API Controller (I followed this SO top answer to figure this step out):

Public Function GetFile(fileInformation As FileDto) As HttpResponseMessage
    Dim filePath = Path.Combine(FileConstants.FilePath, fileInformation.FileType, fileInformation.FileName)
    Dim fileStream = New FileStream(filePath, FileMode.Open, FileAccess.Read)

    Dim result = new HttpResponseMessage(HttpStatusCode.OK)
    result.Content = New StreamContent(fileStream)
    result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream")

    Return result
End Function

The returned result from this call is of HttpResponse, and the body is of type Blob.

HttpResponse: {
  blob: {
    size: 289940,
    type: "application/octet-stream"
  headers: ...,
  status: 200,

How do I now trigger a download for this file in the Angular component where I receive the response?


install FileSaver from npm.

npm install --save file-saver

import it into your class where you have the blob instance

import * as FileSaver from 'file-saver';

Then just call

FileSaver.saveAs(blob, fileName);

Leave a Reply

(*) Required, Your email will not be published