Angular4 how to download file






















If you are not using stricter type then your code should be working fine as it is working for Angular First replace the line this. I am also accepting response as Blob Binary Large Object. You may also specify any value from supporting values, such as, json, blob, arraybuffer, text. You can have a look for more details on response type. I have used three ways for downloading file — two ways for Save as functionality and one way to show the file content on browser itself.

The above line create a Blob object with file content in response and expecting the file of JSON type. The above two lines create a URL that will open the file in browser in new window. The above line shows the file content on browser, so it does not give you save as option.

The above line uses ready-made FileSaver module that will open the file with Save as option. I have created service class to fetch file data from a server URL but I need to provide a link or button for downloading the file. In the view file I will give users two options for downloading the same file.

I will use link as well as button for downloading the same file from the server. In the service class I have used Http module which may not be found automatically. So I need to register it in providers array of NgModule. Run the Angular application angular-file-download by executing command ng serve --open. This way we can access event-specific fields in a type-safe way.

They both contain the discriminator field type allowing us to easily return a boolean for the type assertion in our guards. The guards can be used with a simple if-statement, however, TypeScript will narrow the event type inside the statement block for us:. Based on these guards we can now create our custom operator.

It'll leverage scan , an operator that allows us to accumulate state for successive values coming through an observable. It takes up to two arguments: First, we provide an accumulator function which will compute the next Download state from the previous one and the current HttpEvent.

Second, we'll pass a seed to scan representing the initial Download state. This seed will represent our download being pending without any progress or content:. Our accumulator will use the previously defined guard to update the Download state over time with information from the HTTP events:.

When we encounter a HttpProgressEvent , we calculate the progress based on the number of bytes already loaded and the total bytes. A download is done when we receive a HttpResponse containing the file contents in its body. When receiving any other events than HttpProgressEvent or HttpResponse , we won't alter the download's state and return it as it is.

This way, for example, we can keep the information in the progress property while other events that won't allow us to compute the progress can be ignored for now.

Anything unclear? Let's finally define our custom operator that's using scan with our accumulator and seed :. Notice that this download operator accepts an optional parameter saver. Once a HTTP response is received, this function is invoked with the download content from inside the accumulator.

This allows us to pass in a strategy for persisting the download to a file without directly coupling the operator to FileSaver. By keeping FileSaver. The download operator can be tested without somehow mocking the saveAs import see here for corresponding tests. If we apply the same pattern to the service, we'll be able to test it just as easy. So let's do that by creating a custom injection token for saveAs in a file called saver.

Let's use the Progress Bar from Angular Material to show how far along our download is. The component now only has to assign an observable download to this property:. We can then subscribe to this observable through the AsyncPipe in combination with NgIf. While the download is pending we'll display the progress bar in 'buffer' mode you may also use 'query' , otherwise the progress is determinate.

The bar's value can then easily be applied from Download. Notify me via e-mail if anyone answers my comment. Skip to content Post author: Umut Esen Post category: Angular Reading time: 2 mins read In this post, I will show you how easy it is to download a file with Angular using file-saver package. Tags: angular 7 , download blob with angular , download file with angular , npm , typescript.

Enjoyed it? Share the love! Share this content Opens in a new window Opens in a new window Opens in a new window Opens in a new window Opens in a new window. He is currently working as a senior software developer in Edinburgh, UK. He is the primary author and the founder of onthecode. Noe 19 Dec Reply. Excelente Post. Me salvo la vida. Leave a Reply Cancel reply Comment.



0コメント

  • 1000 / 1000