excel - Angular 5 - 如何使用 HTTPRespose.blob()?
问题描述
在 Angular 4 中,我曾经通过 angular 4 http 库进行 http get 或 post 调用,将任何 excel 或 pdf 文件下载为 blob。
我在 Angular 4 中的代码曾经看起来像这样:
import { Http, Response, Headers, RequestOptions,ResponseContentType } from '@angular/http';
...................................
getExcelOrPDF(URL: string) {
let headers = new Headers();
headers.append('Content-Type', 'application/json' );
return this.http.get(URL, {headers: headers, responseType: ResponseContentType.Blob
}).map(response => this.getData(response)) .catch(error => this.handleError(error));
}
public getData(res: Response) {
console.log("getData()---Enter");
let headers = res.headers;
let contentType = headers.get('Content-Type');
var blob;
try {
if(contentType === 'pdf' || contentType === 'application/pdf') {
blob = new Blob([res.blob()], { type: "application/pdf"});
} else {
blob = new Blob([res.blob()], { type: "application/vnd.ms-excel"});
}
}
catch (e) {
console.debug(e);
}
console.log("getData()-Exit");
return blob;
}
现在,我们都知道在 Angular 5 中,我们支持 HTTPClient 模块和 HTTPClient,我正试图摆脱 Angular 4 获取数据的方式。所以,我已经导入了 HTTPClient、HTTPResponse 等。
我在 Angular 5 中的代码如下所示:
import { HttpClient, HttpResponse , HttpHeaders} from '@angular/common/http';
...................................
getExcelOrPDF(URL: string) {
let headers = new HttpHeaders();
headers.append('Content-Type', 'application/json' );
return this.http.get(URL, {headers : headers, responseType: 'blob' as 'json'
}).map(response => this.getData(response)) .catch(error => this.handleError(error));
}
public getData(res: HttpResponse<any>) {
console.log("getData()---Enter");
let headers = res.headers;
let contentType = headers.get('Content-Type');
var blob;
try {
if(contentType === 'pdf' || contentType === 'application/pdf') {
blob = new Blob([res.blob()], { type: "application/pdf"});
} else {
blob = new Blob([res.blob()], { type: "application/vnd.ms-excel"});
}
}
catch (e) {
console.debug(e);
}
console.log("getData()-Exit");
return blob;
}
但是,在 angular 4 代码完美运行的地方,上面的 angular 5 代码给了我几个错误:
- res.blob() --> 使用 HTTPResponse ,我得到错误:
blob() function is not supported
。这是我在编辑器中看到的错误。 - 当代码运行时,它编译成功,但是当我尝试下载 excel 时,出现错误:
unsupported media type : 415
- 我使用 console.logs 检查,但似乎函数 getData() 甚至没有被调用,因为控制台日志没有被打印。
有人可以让我知道我在这里做错了什么吗?任何帮助都深表感谢。
解决方案
我犯了一个愚蠢的错误,因此我面临很多问题。
首先:当我们使用 HTTPClient 而不是 HTTP 时,我们应该像这样创建标头:
let headers = new HttpHeaders();
headers = headers.append('Content-Type', 'application/json' );
需要将 headers 对象分配回 headers ,否则实际的 headers 对象将为空。
其次:像这样进行http调用:this.http.get(URL, {headers : headers, observe: 'response', responseType: 'blob'}
第三: res.blob()
在 HTTPResponse 中不可用。相反,我们应该使用:res.body
经过互联网上数百篇文档和文章以及无数次尝试和错误,我能够想出一个令人满意的解决方案。
希望这对遇到类似问题的其他人有所帮助。
推荐阅读
- linux - Terraform错误运行shell脚本时替换错误
- python - 将更改变量写入 csv 文件到不同的行
- javascript - 如何在javascript中更新存储在firebase数据库中的嵌套数组?
- google-bigquery - 在加载到 BigQuery 之前在 Google Cloud Storage 上暂存数据是否(仍然)有优势?
- java - Spring Boot 使用 antMatchers 动态加载授权 URL
- android - 是否有更改日期选择器表单的选项?
- radio-button - UFT:单选按钮的 SetCellData 问题 PbDataWindow
- vega-lite - Vega-Lite:我可以合并数据源吗?
- c# - 未捕获的 ReferenceError:未定义 OnClickCallback ASP.NET Blazor 客户端
- sql - 根据其他 3 个列(年、月、日)创建一个包含日期的新列