Angular 5.2自定义HttpClient类-无法将responseType更改为blob
我在我的项目中使用Angular 5.2版本,将默认响应类型更改为带有新函数的blob时遇到了问题 现在我想将responseType更改为blob以下载excel文件,但它对我不起作用 在我的项目中,我创建了包装器类CustomHttpClient来进行Get或Post调用 CustomHttpClient类如下所示:-Angular 5.2自定义HttpClient类-无法将responseType更改为blob,angular,angular5,angular-httpclient,Angular,Angular5,Angular Httpclient,我在我的项目中使用Angular 5.2版本,将默认响应类型更改为带有新函数的blob时遇到了问题 现在我想将responseType更改为blob以下载excel文件,但它对我不起作用 在我的项目中,我创建了包装器类CustomHttpClient来进行Get或Post调用 CustomHttpClient类如下所示:- export interface IRequestOptions { headers?: HttpHeaders; observe?: 'body'
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);
}
}
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);
}
现在我想调用GetBlob函数,并将responseType更改为'blob'。但它向我展示了错误类型blob不能分配给Json类型
在组件服务类中,我这样调用fn:-
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);
}
}
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);
}
尝试更改iRequestStoptions接口,使响应类型为字符串类型。 像这样:
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类型检查中作弊