【发布时间】:2020-02-21 10:05:51
【问题描述】:
我有一个端点,它返回一个 csv 数据集,它将使用 saveas 和 Blob 保存
AngularJS 前端工作正常,而 Angular 8 却在挣扎。
这是我的 Angular 8 实现
const headers = new HttpHeaders({
Accept: 'text/csv',
});
const options = { headers };
this.httpClient
.post<ExportCsvSettingsViewDto>(this.appConfigService.buildApiUrl(this.loadPath(batchId)), csvDto, options)
.subscribe((fileResult: any) => {
const file = new Blob([fileResult], { type: 'text/csv' });
saveAs(file, this.fileName + '.csv');
});
这导致HttpErrorResponse 声明:
对象 {error: SyntaxError: Unexpected token 。在 JSON 中的位置...,文本:“1.112.373;en-US
日期时间 UTC;日期本地时间;蝙蝠…"}
我怀疑这是因为HttpClient 将响应视为JSON 而不是"text/csv"
这些是请求标头:
Connection: keep-alive
Content-Length: 141
Pragma: no-cache
Cache-Control: no-cache
Accept: text/csv
Content-Type: application/json
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
和响应头:
HTTP/1.1 200 OK
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Content-Type: text/csv
Expires: 0
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET
X-UA-Compatible: IE=edge
Content-Length: 191147
我尝试将请求标头中的"responseType" 显式设置为"text/csv",但这是不可能的。
有什么想法吗?
【问题讨论】:
标签: angular http-headers httpclient