Using Angular 7, I am calling API by get method and try to download file by using 'saveAs' function from the fileSaver library.
when I getting filename from response header then unable to download the proper file and getting[Object object] in the file content but whenever using dummy name it Works fine.
it is component file:
fileName:string="";
const MIME_TYPE ={
XLSX:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
TXT:'text/*',
PDF:'application/pdf'
}
downloadFile(){
this.DownloadService.downloadFile(
(data: any) => {
this.fileName=data.headers.get('fileName');
const EXT = this.fileName.substr(this.fileName.lastIndexOf('.')+1);
saveAs(new Blob([data],{type:MIME_TYPE[EXT]}),this.fileName);
}
);
}
DownloadService file
public downloadFile(callback: (responseData: any) => void): void {
this.apiService.downloadFile.subscribe((data: any) => {
if (callback) {
callback(data);
}
});
}
apiService file
downloadFile(param: any): Observable<any> {
return this.http.get(api/download, {responseType:'blob',observe: 'response' });
}
Output in file content
[Object object]