无法在Angular 2中的api调用响应中下载.xlsx文件?

时间:2017-11-22 06:41:57

标签: angular

我正在尝试下载' .xlsx'来自api调用的响应的文件以角度为2.在响应正文中我将数据作为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我正在获取如下数据

enter image description here

所以我不知道如何将xml类型的响应体下载到' .xlsx'格式。

1 个答案:

答案 0 :(得分:0)

现在正在运作。

我在请求对象中添加了FROM openjdk:8-jdk-alpine VOLUME /tmp ADD target/app-1.0-final.jar app.jar ENTRYPOINT ["java", "-jar", "app.jar"] EXPOSE 8080

并在使用前将responseType: ResponseContentType.Blob导入服务文件,如下所示。

ResponseContentType

此后没有"损坏的文件错误"显示和excel文件数据正常。