Error Encountered While Trying to Download Excel File via API
I attempted to download an Excel file through an API using a specific method, but unfortunately, I encountered the following error:
SyntaxError: Unexpected token P in JSON at position 0 at JSON.parse () at XMLHttpRequest.onLoad (http://localhost:5002/vendor.js:28768:51)
Here is my code snippet:
getSalesARExcel(attach: string): Observable<any>{
const obj={
"AttachmentId":attach
}
return this.http.post(baseUrl + 'api/report/aging/receivables/summary/download/xlsx', obj ,{
headers: new HttpHeaders({
'Content-Type': 'application/json',
"Authorization":'Bearer ' + localStorage.getItem('token'),
'Access-Control-Allow-Origin':'*',
'Access-Control-Allow-Headers':'Origin, Methods, Content-Type',
'responseType': 'ResponseContentType.Blob'
})
})
}
this.excel.getSalesARExcel(value.attach).subscribe(res => {
console.log("excel", res)
this.downloadExcelFile(res);
})
downloadExcelFile(data: any){
var blob = new Blob([data], { type: 'application/vnd.ms-excel' });
var url= window.URL.createObjectURL(blob);
window.open(url);
}