【问题标题】:To accept response of type ZIP from API using angular 7?使用 Angular 7 接受来自 API 的 ZIP 类型的响应?
【发布时间】:2020-05-14 12:07:02
【问题描述】:

我正在尝试使用 API 响应将 zip 文件下载到我的本地系统,使用 angular 的类型为 zip。但由于它是 zip 类型,我无法接受,而是遇到错误。

a.service.ts

 download() {
        const headers: HttpHeaders = new HttpHeaders({
            "content-type": "application/json",
            "Accept": "application/zip"
        });
        return this.http.get(url, {headers: headers, responseType: "arraybuffer"});
    }

a.component.ts

getDownload() {
        console.log("Hello world");
        this.Service.download().subscribe((responseData: any) => {
        console.log(responseData);
        },(error: any) => {
        });

错误:

尝试将 responseType 添加为“blob”、“text”和“arraybuffer”...似乎没有任何效果

【问题讨论】:

  • 尝试使用blob 而不是arraybuffer,并删除您的标题
  • 能否从网络选项卡中显示请求标头的屏幕截图?
  • @David 是添加
  • 是去掉标题之后吗?
  • @David 不,在此之前...删除标题后要检查的构建图像将更新

标签: javascript angular typescript zip


【解决方案1】:

您需要从请求中删除标头并将 responseType 设置为 blob

如果您指定这些标头,这将覆盖来自 blob 响应类型的默认解析并尝试将 zip 文件解析为 json 数据

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    相关资源
    最近更新 更多