【发布时间】:2019-03-22 02:49:31
【问题描述】:
我正在尝试从角度 2 中的 api 调用的响应中下载“.xlsx”文件。作为响应,正文将数据作为 xml 格式获取。它正在下载,但打开文件后会出现文件损坏等错误。请在下面找到示例代码。
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
});
在response._body 我正在获取如下数据
所以我不知道如何将 xml 类型的响应正文下载为“.xlsx”格式。
【问题讨论】:
标签: angular