文件这是使POST请求与响应的xls文件我的服务的代码:这是为了处理Excel文件的响应如何正确下载的Excel与Angular2
exportInternalOrder(body) {
let user_token: string = this.sessionService.getToken();
let headers = new Headers();
headers.append('responseType', 'arraybuffer');
headers.append('Authorization', 'Bearer ' + user_token);
return this.http.post(this.config.exportInternalOrder, body,{
headers: headers
}).map(res => new Blob([res._body],{ type: 'application/vnd.ms-excel' }));
}
。 这是代码调用它:
let objToSend = this.makeObjToSend(false);
this.reportingService.exportExcel(objToSend)
.subscribe(
data => {
this.exportData(data);
},
error => {
this.errorFilterMsg.push({ severity: 'error', detail: 'Report exporting has failed!' });
}
);
这是文件的保存(出于某种原因window.open什么都不做):
exportData(data){
let blob = data;
let a = document.createElement("a");
a.href = URL.createObjectURL(blob);
a.download = 'fileName.xls';
document.body.appendChild(a);
a.click();
}
但文件仍然保存为一个已损坏。当使用邮差和卷曲它来确定。任何帮助,将不胜感激。
您能告诉我们'exportData(data)'中的'data'吗?只是想检查'data'中的内容。 –
这是对我工作:https://stackoverflow.com/questions/41771041/angular2-download-excel-file-from-web-api-file-is-corrupt/44680057#44680057 – Deepak
[Angular2下载的可能的复制从Web API的Excel文件,文件已损坏](https://stackoverflow.com/questions/41771041/angular2-download-excel-file-from-web-api-file-is-corrupt) –