类型“Object”上不存在Blob错误-使用Angular 4从post调用下载excel文件
前端:角度4 后端:JavaRESTfulWeb服务 我的rest调用返回一个excel作为响应。我已经编写了将同一文件返回给用户的代码 我的控制器:类型“Object”上不存在Blob错误-使用Angular 4从post调用下载excel文件,angular,http-post,blob,angular2-observables,angular4-httpclient,Angular,Http Post,Blob,Angular2 Observables,Angular4 Httpclient,前端:角度4 后端:JavaRESTfulWeb服务 我的rest调用返回一个excel作为响应。我已经编写了将同一文件返回给用户的代码 我的控制器: exportExcelXSL() { var excelData; console.log("Excel"); console.log(this.workRequestSearchBasicReq); this.workRequestSearchBasicReq.OrdersPerPage = undefined;
exportExcelXSL() {
var excelData;
console.log("Excel");
console.log(this.workRequestSearchBasicReq);
this.workRequestSearchBasicReq.OrdersPerPage = undefined;
let request = Utility.removeUndefinedNullFields(this.workRequestSearchBasicReq);
console.log(request);
var data = {};
data["ICWRQry"] = Utility.removeUndefinedNullFields(request);
this.workRequest.workRequestReport(data).subscribe(data => {
var mediaType = 'application/vnd.ms-excel';
let blob: Blob = data.blob();
window['saveAs'](blob, 'sample.xls');
});
this.cd.detectChanges();
}
我的服务:
workRequestReport(searchOrderReq: any): Observable<Blob> {
let workRequestSearchBeanShellUrl = "http://localhost:7001/uuigui/uui/eapp/iVAPP/WorkRequestSearchReport";
return this.httpClient.post(workRequestSearchBeanShellUrl, searchOrderReq, {
headers: new HttpHeaders({'responseType': 'ResponseContentType.Blob '})
}).map(data => data.blob());
}
但在控制台中,由于ServiceLine:mapdata=>data.blob;&ControllerLine:let blob:blob=data.blob
有人能帮我解决这个问题吗
提前感谢。您可以将workRequestReport方法更新为以下内容
workRequestReport(searchOrderReq: any): Observable<HttpResponse<Blob>> {
let workRequestSearchBeanShellUrl = "http://localhost:7001/uuigui/uui/eapp/iVAPP/WorkRequestSearchReport";
return this.httpClient.post(workRequestSearchBeanShellUrl, searchOrderReq, {
observe: 'response',
responseType: 'blob'
});
}
我只能对服务调用进行评论,如果使用修改后的服务方法,就可以了。如果希望在saveAs块之后调用this.cd.detectChanges,请将其移动到subscribe块中。