download excel template file to browser in angular6

486 Views Asked by At

i did try to download excel template file in angular6 using spring boot. in rest api file is getting download properly but when integrate rest api with angular excel file is not getting download. Please any one can help

From angular to api is calling very fine

servivce.ts file

 download(data){
    const REQUEST_PARAMS=new HttpParams().set('fileName',data.fileName);
    const REQUEST_URI='/downloadTemplate'; 
    // don't have any issue with url as eddit by me
     return this.http.get(REQUEST_URI,{
        params:REQUEST_PARAMS,
        responseType:'arraybuffer'
      })
    }

component file

download(){
    this.fileName='download.xlsx';
    const EXT=this.fileName.substr(this.fileName.lastIndexOf('.')+1);
    this.service.download({'fileName': this.fileName})
   .subscribe(response=>{
    saveAs(new Blob([response], {type:'application/vnc.openxmlformats-        officedocumnet.spreadsheetxml.sheet'}), this.fileName);
   })
  }
1

There are 1 best solutions below

0
Adrita Sharma On

You need to add { responseType: 'blob' }

Try like this:

service.ts

download(data){
    return  this.http.get(REQUEST_URI, { responseType: 'blob' })
}

component.ts

this.service.download({'fileName': this.fileName})
.subscribe(response=> {
    saveAs(response, this.fileName);
})