I've an API which returns an excel document as response. The request will be one simple json.
I've searched google and found some code base to download the file and I've used it in my application, but I'm getting some errors and unable to find it out the solution. Below are my code base.
component.ts
import rxjs/Rx;
details = {id: 75,name: "some name"}
this.nameService.getData(this.details).subscribe(response => {
this.downloadFile(response);
})
downloadFile(data: Response) {
const blob = new Blob([data], { type: '.xlsx' });
const url= window.URL.createObjectURL(blob);
window.open(url);
}
nameService.ts
getData(postData) {
return this.httpService.post('https://localhost:8080/getFile/', postData);
}
httpService.ts
constructor(private http: HttpClient)
post(apiEndpoint: string, request: any) :Observable<any> {
return this.http.post<any>(apiEndpoint,request);
}
with the above code base I'm getting below two errors and the file is not downloaded.
- Getting the error :
"Type Response is not assignable to type Blobpart" in creating the Blob(const blob = new Blob([data], { type: '.xlsx' });)
- If I change the data as any
(downloadFile(data: any))
the above error(1) will go but I'm getting anhttperror
response as 'Syntax error: unexpected token P in json at position 0 atjson.parse
Kindly help if anyone finds any solution to the above issues.