When using Angular 7, I am making an API call by posting the URL file and attempting to download it using the 'saveAs' function from the fileSaver library. The file is successfully downloading, but it appears to be corrupted and cannot be opened.
My code for the call is as follows:
var file_url = (response as any).headers['Location'] + 'files/Data.xlsx';
var filename = 'Data_' + this.getDateService.getDateFile() + '.xlsx';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded'
}),
responseType: 'arraybuffer',
observe: 'response'
};
let downloadParameters = { filename: 'Data_' + this.getDateService.getDateFile() + '.xlsx', file: file_url }
this.downloadFileService.downloadFile(downloadParameters, httpOptions).subscribe(response => {
var blob = new Blob([(response as any).body], { type: 'application/vnd.openxmlformat-officedocument.spreadsheetml.sheet' });
saveAs(blob, filename);
})
Things I have tried:
- Chaning the Type MIME from application/vnd.openxmlformat-officedocument.spreadsheetml.sheet to application/octet-stream
- Switching the responseType from arraybuffer to blob or blob as json
Below are the response headers from the service:
https://i.sstatic.net/BUU7E.png
The file content in the response body:
https://i.sstatic.net/WP0Qq.png
Does anyone have any insights on how to fix this issue?