2

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?

1
  • 1
    in Angular2 (4) I simply installed FileSaver and then imported it into my class and then ran FileSaver.saveAs(blob, fileName); Commented Oct 27, 2017 at 19:07

2 Answers 2

23

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);
Sign up to request clarification or add additional context in comments.

4 Comments

You saved me a lot of ugly code with this package. Thanks!
This package saved me a lot of ugly code, as well. :D
For Angular 10 there is warning: "CommonJS or AMD dependencies can cause optimization bailouts." due to using file-saver.
@GregorAlbert Oh no! A warning? What a nightmare!
5

As @Jason Spradlin pointed out you can use FileSaver package

There is another package to save file :

npm install @progress/kendo-file-saver --save

Usage:

import { saveAs } from '@progress/kendo-file-saver'

saveAs(data: string | Blob, fileName: string, options?: SaveOptions)

1-Data is base64

const result = `data:${YOUR_MIME_TYPE};base64,` + YOUR_DATA;
saveAs(result, YOUR_FILE_NAME);

2-Data is Blob

saveAs(YOUR_DATA, YOUR_FILE_NAME);

Manually:

const file = new window.Blob([data], { type: contentType });

const downloadAncher = document.createElement("a");
downloadAncher.style.display = "none";

const fileURL = URL.createObjectURL(file);
downloadAncher.href = fileURL;
downloadAncher.download = fileName;
downloadAncher.click();

1 Comment

Will new window trigger popup blockers? (in 'manually' code). (Also: it's anchor with an o =P)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.