Reputation: 21
I want to download an excel file on response: this what I did :
download(type: string) {
this.http.get(`download_template?template=${type}`).subscribe(data => {});
}
and this is what I got :
Upvotes: 2
Views: 6474
Reputation: 3099
In your backend make an endpoint which returns the file you wanted when requesting.
In your front end use in this way
<a [href]=URLoftheFile download="FilenameWithExtension">DownloadFile</a>
Upvotes: 1
Reputation: 41
Have done something similar... This is my approach below and it works.
Install npm i --save file-saver
Service
download(type: string) {
return this.http.get(`download_template?template=${type}`, {responseType: 'blob'});
}
Component
...
import { saveAs } from 'file-saver';
...
this._service.download(type: string).subscribe(res => {
saveAs(res, 'YourFileName.xlsx',
{ type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
}}
Upvotes: 3