I am trying to download '.xlsx' file from the response of the api call in angular 2. In response body am getting the data as xml format. It is downloading but after opening the file it gives error like corrupted file. Please find below sample code.
this.homeDetailViewService.downloadReport(JSON.stringify(requestBody))
.subscribe((response) => {
if (response) {
console.log(response);
var contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
let blob = new Blob([response._body],{type:contentType});
let link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = "report.xlsx";
link.click();
}
},
(error) => {
let errorObj = JSON.parse(error);
// If theres no data then reset the model list and total
if (errorObj.status === "error" && errorObj.message.match(/Record not found/)) {
this.curData = null;
}
this.loadingAnimation = false; // error path
this.handleError(error);
},
() => {// onComplete
});
In response._body
am getting data like below
So i don't know how to download xml type response body into '.xlsx' format.
It is working now.
I added responseType: ResponseContentType.Blob
in the request object.
And import ResponseContentType
to the service file before use as below.
import { ResponseContentType } from '@angular/http';
After this no "Corrupt file error" is showing and excel file data coming fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.