angular5 - “HttpResponse”类型上不存在属性“blob”'
问题描述
我正在尝试将 Angular 4 升级到 5,为此我必须从 @angular/common/http 导入 HttpClient、HttpResponse、HttpHeaders,而不是从“@angular/http”导入 Http、Response、Headers、RequestOptions、ResponseContentType;
但在我之前的代码中,我使用的是 let headers = new Headers(); 我替换为 let headers = new HttpHeaders(); 和 responseType: ResponseContentType.Blob 替换为 responseType: 'blob'
现在在方法 getData() res.blob() 中说“HttpResponse”类型上不存在属性“blob”。
ExcelDownload(_getRequestUrl: string) {
let headers = new HttpHeaders();
headers.append('Content-Type', 'application/json' );
return this.http.get(this.getUrlWithEmpId(_getRequestUrl), {
headers : headers,
responseType: 'blob'
}).map(response => this.getData(response)) .catch(error => this.handleError(error));
}
public getData(res: HttpResponse<any>) {
let headers = res.headers;
let contentType = headers.get('Content-Type');
var blob;
try {
if(contentType === 'pdf' || contentType === 'application/pdf') {
//chek this
blob = new Blob([res.blob()], { type: "application/pdf"});
} else {
//check thi
blob = new Blob([res.blob()], { type: "application/vnd.ms-excel"});
}
}
catch (e) {
}
return blob;
}
解决方案
推荐阅读
- powershell - Powershell if 语句从 csv 进入 foreach 循环
- discord - 使用包文件创建不和谐机器人时出错
- r - 如何在 R 中合并此数据集?
- reactjs - FormatJS / React-intl 中的 if/else
- javascript - 每次出现在字符串中时,用新值替换部分字符串,JavaScript
- angular - 如何在 Angular 中保持 FormGroup 的状态
- javascript - 为什么在 asp.net mvc 中加载模型很慢?
- java - 在 Spring Boot 中使用环境变量
- actions-on-google - 具有相同命令的设备
- javascript - CSS 样式不适用于 React 标题和段落