Angular 5.2 自定义 HttpClient class - 无法将响应类型更改为 blob
Angular 5.2 Custom HttpClient class - Unable to change the responseType to blob
我在我的项目中使用 Angular 5.2 版本并且 运行 将默认 responseType 更改为具有新功能的 blob 存在问题。
现在我想将 responseType 更改为 blob 以下载 excel 文件,但它对我不起作用。
在我的项目中,我创建了包装器 class 作为 CustomHttpClient 来进行 Get 或 Post 调用。
CustomHttpClient class 看起来像这样:-
export interface IRequestOptions {
headers?: HttpHeaders;
observe?: 'body';
params?: HttpParams;
reportProgress?: boolean;
responseType?: 'json';
withCredentials?: boolean;
body?: any;
}
/*Custom HttpClient Class for making the WebAPI calls.*/
export class CustomHttpClient {
_defaultHeaderType = new HttpHeaders({ 'Content-Type': 'application/json' });
// Extending the HttpClient through the Angular DI.
public constructor(public _http: HttpClient) {
}
public Get<T>(endPoint: string, options?: IRequestOptions): Observable<T> {
if (options == undefined) {
options = <IRequestOptions>{};
options.headers = this._defaultHeaderType
}
options.headers = this.injectClient(options.headers);
return this._http.get<T>(endPoint, options);
}
public GetBlob<T>(endPoint: string, options?: IRequestOptions): Observable<T> {
if (options == undefined) {
options = <IRequestOptions>{};
options.headers = this._defaultHeaderType
}
//This line is giving error
options.responseType = 'blob';
options.headers = this.injectClient(options.headers);
return this._http.get<T>(endPoint, options);
}
}
现在我想调用 GetBlob 函数并将 responseType 更改为 'blob'。但它向我显示错误 "Type "blob" is not assignable to type "Json"".
从组件服务 class,我这样调用 fn :-
downloadTemplate(): Observable<any> {
let opt = {
headers: new HttpHeaders({ 'Content-Type': 'application/json', 'Accept': 'application/xlsx'}),
options: new RequestOptions({ responseType: ResponseContentType.Blob })
}
return this._httpObj.GetBlob(this._baseUrl + 'DownloadTemplate', opt);
}
尝试更改您的 RequestOptions 接口,使响应类型为字符串类型。
像这样:
export interface IRequestOptions {
headers?: HttpHeaders;
observe?: 'body';
params?: HttpParams;
reportProgress?: boolean;
responseType?: string;
withCredentials?: boolean;
body?: any;
}
最近遇到了同样的问题,希望他们能尽快解决。
我使用 Object.assign 作为解决方法:
this._http.get<T>(endPoint, Object.assign(options, { responseType: 'blob' }))
这样你就可以在 ts 类型检查上作弊。
我在我的项目中使用 Angular 5.2 版本并且 运行 将默认 responseType 更改为具有新功能的 blob 存在问题。
现在我想将 responseType 更改为 blob 以下载 excel 文件,但它对我不起作用。
在我的项目中,我创建了包装器 class 作为 CustomHttpClient 来进行 Get 或 Post 调用。
CustomHttpClient class 看起来像这样:-
export interface IRequestOptions {
headers?: HttpHeaders;
observe?: 'body';
params?: HttpParams;
reportProgress?: boolean;
responseType?: 'json';
withCredentials?: boolean;
body?: any;
}
/*Custom HttpClient Class for making the WebAPI calls.*/
export class CustomHttpClient {
_defaultHeaderType = new HttpHeaders({ 'Content-Type': 'application/json' });
// Extending the HttpClient through the Angular DI.
public constructor(public _http: HttpClient) {
}
public Get<T>(endPoint: string, options?: IRequestOptions): Observable<T> {
if (options == undefined) {
options = <IRequestOptions>{};
options.headers = this._defaultHeaderType
}
options.headers = this.injectClient(options.headers);
return this._http.get<T>(endPoint, options);
}
public GetBlob<T>(endPoint: string, options?: IRequestOptions): Observable<T> {
if (options == undefined) {
options = <IRequestOptions>{};
options.headers = this._defaultHeaderType
}
//This line is giving error
options.responseType = 'blob';
options.headers = this.injectClient(options.headers);
return this._http.get<T>(endPoint, options);
}
}
现在我想调用 GetBlob 函数并将 responseType 更改为 'blob'。但它向我显示错误 "Type "blob" is not assignable to type "Json"".
从组件服务 class,我这样调用 fn :-
downloadTemplate(): Observable<any> {
let opt = {
headers: new HttpHeaders({ 'Content-Type': 'application/json', 'Accept': 'application/xlsx'}),
options: new RequestOptions({ responseType: ResponseContentType.Blob })
}
return this._httpObj.GetBlob(this._baseUrl + 'DownloadTemplate', opt);
}
尝试更改您的 RequestOptions 接口,使响应类型为字符串类型。 像这样:
export interface IRequestOptions {
headers?: HttpHeaders;
observe?: 'body';
params?: HttpParams;
reportProgress?: boolean;
responseType?: string;
withCredentials?: boolean;
body?: any;
}
最近遇到了同样的问题,希望他们能尽快解决。 我使用 Object.assign 作为解决方法:
this._http.get<T>(endPoint, Object.assign(options, { responseType: 'blob' }))
这样你就可以在 ts 类型检查上作弊。