Angular download file from assets






















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. When you click on link or button for downloading file you will see below page with file save option:.

When you use the code for displaying data on browser inside download function of controller code and click on button or link, then you should see below output:. Don't you have to somehow remove the link from DOM? However, I cannot find a reliable way to work around that. How would you fix it? Show 6 more comments. I hate to post the simple thank you comments as they are usually not value added I had been searching struggling with this for WAY too many hours today.

I had been searching for the code you have from this. Thanks, works without needing any plugins etc. Saved my day, big thumbs up for you. You should pass this. Show 4 more comments.

Andreo Andreo 11 3 3 bronze badges. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Who owns this outage? Building intelligent escalation chains for modern SRE. Podcast Who is building clouds for the independent developer? Featured on Meta. Now live: A fully responsive profile. To use in Angular, we can convert the href and download attributed to directives to take in dynamic values as shown below:.

Note: Due to security reasons the download attribute follows same-origin policy so we need to provide same domain of file to be downloaded otherwise the download attribute will be ignored. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 3 years, 9 months ago. Active 7 months ago. Viewed 5k times.

Ice Bunny Ice Bunny 67 1 1 silver badge 9 9 bronze badges.



0コメント

  • 1000 / 1000