【发布时间】:2020-07-06 21:33:00
【问题描述】:
我有一个应用程序调用一个返回文件并将它们压缩成 zip 文件的 api。
我创建了自己的服务,它运行良好。
return this.http.get(this.rootUrl + '/api/Files/DownloadFile?files=' + fileSelected + '&user=' + user, { responseType: 'blob' as 'json' }); // working ok
这里的 api 响应必须是一个 blob 对象。
但是当我尝试使用 swagger 生成的服务做同样的事情时,它不再起作用了。
let req = new HttpRequest<any>(
'GET',
this.rootUrl + `/api/Files/DownloadFile`,
__body,
{
headers: __headers,
params: __params,
responseType: 'text'
}
); // not working ok
服务返回 null,但是在 chrome 检查器中,在对 api 调用的响应中,它显示奇怪的字符。
this.filesService.DownloadFile({"files": ids, "user": this.user}).subscribe(
data => {
console.log(data); // data value is null but in the response I see strange characters
},
err => {
console.log(err);
}
);
这是正常的,因为swagger服务的responseType是“text”,我试过手动把contentType改成“blob”(swagger规范)或者“blob as json”(angular 5),但是没有工作正常。
是否可以使用 swagger 生成的服务从 Angular 解决它,还是我必须触摸 API?谢谢
【问题讨论】:
-
我也有同样的问题 :((, 不能使用 responseType blob 太可惜了。