Mustafa El Menshawy
Mustafa El Menshawy

Reputation: 21

Angular 6 download API excel file

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

Answers (2)

dasunse
dasunse

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

Saddy O.
Saddy O.

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

Related Questions