我正在尝试将responseType设置为arraybuffer,但是它给出了错误。 api返回一个文件,我需要捕获该文件并生成文件。
我正在调用如下的exportCartDetails,它是在Mat对话框的onAdd事件发射器上触发的。
const data1 = this.dialogRef.componentInstance.onAdd.subscribe(result => {
this.exportCartDetails(result)
.subscribe(
mydata=>{
console.log('data is all here');
var file =new Blob([mydata],{type: 'application/vnd.ms-excel' });
var fileURL=URL.createObjectURL(file);
let a =document.createElement("a");
a.style.display = "none";
a.href=fileURL;
a.target="_blank";
a.download="CartData.xls";
a.click();
a.remove();
}
)
,
err=>{
alert("Error in calling api");
console.log(err);
};
出口购物车详细信息如下
exportCartDetails(report_data: ReferenceDataApi): Observable<ArrayBuffer> {
const headers=new HttpHeaders().set('Content-Type', '*' );
//header.set( 'Content-Disposition': 'attachment; filename=' + req.body.filename,);
const response = new HttpResponse();
console.log(this.report_data);
return this.http.post<ArrayBuffer>(url,this.report_data,{headers,responseType:'arraybuffer'})
.pipe(
catchError(this.handleError<ArrayBuffer>('exportCartDetails'))
);
}
错误在下面。
ERROR in src/app/cart-detail/cart-detail.component.ts(118,91): error TS2345: Argument of type '{ headers: HttpHeaders; responseType: ArrayBuffer; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'.
Types of property 'responseType' are incompatible.
Type 'ArrayBuffer' is not assignable to type '"json"'.
src/app/cart-detail/cart-detail.component.ts(118,113): error TS2352: Type 'string' cannot be converted to type 'ArrayBuffer'.
答案 0 :(得分:1)
作为解决方法,您可以从httpClient.post()
中删除预期的收益类型return this.http.post(url,this.report_data,{headers,responseType:'arraybuffer'})
.pipe(
catchError(this.handleError<ArrayBuffer>('exportCartDetails'))
);
。
'arraybuffer'
或者,您也可以尝试为responseType:'arraybuffer' as 'arraybuffer'
指定别名,例如
return this.http.post<ArrayBuffer>(url,this.report_data,{headers,responseType:'arraybuffer' as 'arraybuffer'})
.pipe(
catchError(this.handleError<ArrayBuffer>('exportCartDetails'))
);
关于http://192.168.1.186:4444/#/table/basic-table2/articles
的问题尚待解决代码:
{{1}}