首页 > 解决方案 > 通过 Angular 中的 post 请求传递 responseType: 'blob'

问题描述

我通过获取请求传递 responseType: 'blob'。它运作良好。

import { HttpClient, HttpHeaders } from '@angular/common/http';
import { RequestOptions, Response, ResponseContentType } from '@angular/http';


let headers = new HttpHeaders();
headers = headers.set('Content-Type', 'application/pdf');

return this.http.get(url, {
    headers: headers,
    responseType: 'blob'
}

如何通过 post 请求传递相同的 responseType?

我试过了 :

const headers = new Headers({ 
    'Content-Type': 'application/pdf'
});

const options = new RequestOptions({headers: headers});
options.responseType = ResponseContentType.Blob;

return this.http.post(url, body, options)

但它不起作用。我有错误消息:类型参数 '{ headers: Headers; }' 不可分配给“RequestOptionsArgs”类型的参数。属性“标题”的类型不兼容。类型“标题”不可分配给类型“标题”。

UPD 以下评论我已重新提出请求:

   const headers = new HttpHeaders({ 'Content-Type': 'application/pdf'});
    return this.http.post(url, body, { headers, responseType:'blob' })

它运作良好!非常感谢!

标签: angular

解决方案


利用

responseType:'blob' as 'json'

参考:


推荐阅读