admin管理员组文章数量:1416333
How to handle binary file or xlsx as a response from angular ionic. I have done this but I am getting error on when I return the get method.
HTML file:
<ion-button color="primary" expand="block" (click)="downloadPredefinedExcel()" [disabled]="downloadedExcel">
<ion-icon name="download-outline" slot="start"></ion-icon>
Download Excel Template
</ion-button>
Typescript file:
// Download the excel file for bulk upload data
downloadPredefinedExcel() {
thismonService.present();
this.productService.getDownloadExcelForBulkUpload().pipe(
tap((blob: Blob) => {
thismonService.dismiss();
this.downloadedExcel = true;
this.downloadFile(blob, 'BulkUploadTemplate.xlsx'); // Change the filename if needed
thismonService.success('Download Successfully');
}),
catchError((error) => {
thismonService.dismiss();
thismonService.danger('Download failed.');
return throwError(() => new Error(error.message));
})
).subscribe();
}
downloadFile(blob: Blob, filename: string) {
const link = document.createElement('a');
const url = window.URL.createObjectURL(blob);
link.href = url;
link.download = filename;
link.click();
window.URL.revokeObjectURL(url);
}
Service file:
/* For Download Excel Bulk Upload */
getDownloadExcelForBulkUpload() {
return thismonApi.getDownloadExcelForBulkUpload();
}
common file:
getDownloadExcelForBulkUpload() {
return this.getBlob(`${environment.baseUrl + ApiUrls.DownloadExcelForBulkUpload}`);
}
GenericApi API file
protected getBlob(url: string, options: any = {}): Observable<Blob> {
return this.http.get<Blob>(url, { ...options });
}
ApiUrls file:
DownloadExcelForBulkUpload: '/api/files/bulkUpload/ServiceDownload',
But I am getting this error on GenericApi API file:
Type 'Observable<HttpEvent<Blob>>' is not assignable to type 'Observable<Blob>'.
Type 'HttpEvent<Blob>' is not assignable to type 'Blob'.
Type 'HttpSentEvent' is missing the following properties from type 'Blob': size, arrayBuffer, bytes, slice, and 2 more.ts(2322)
How to handle binary file or xlsx as a response from angular ionic. I have done this but I am getting error on when I return the get method.
HTML file:
<ion-button color="primary" expand="block" (click)="downloadPredefinedExcel()" [disabled]="downloadedExcel">
<ion-icon name="download-outline" slot="start"></ion-icon>
Download Excel Template
</ion-button>
Typescript file:
// Download the excel file for bulk upload data
downloadPredefinedExcel() {
thismonService.present();
this.productService.getDownloadExcelForBulkUpload().pipe(
tap((blob: Blob) => {
thismonService.dismiss();
this.downloadedExcel = true;
this.downloadFile(blob, 'BulkUploadTemplate.xlsx'); // Change the filename if needed
thismonService.success('Download Successfully');
}),
catchError((error) => {
thismonService.dismiss();
thismonService.danger('Download failed.');
return throwError(() => new Error(error.message));
})
).subscribe();
}
downloadFile(blob: Blob, filename: string) {
const link = document.createElement('a');
const url = window.URL.createObjectURL(blob);
link.href = url;
link.download = filename;
link.click();
window.URL.revokeObjectURL(url);
}
Service file:
/* For Download Excel Bulk Upload */
getDownloadExcelForBulkUpload() {
return thismonApi.getDownloadExcelForBulkUpload();
}
common file:
getDownloadExcelForBulkUpload() {
return this.getBlob(`${environment.baseUrl + ApiUrls.DownloadExcelForBulkUpload}`);
}
GenericApi API file
protected getBlob(url: string, options: any = {}): Observable<Blob> {
return this.http.get<Blob>(url, { ...options });
}
ApiUrls file:
DownloadExcelForBulkUpload: '/api/files/bulkUpload/ServiceDownload',
But I am getting this error on GenericApi API file:
Type 'Observable<HttpEvent<Blob>>' is not assignable to type 'Observable<Blob>'.
Type 'HttpEvent<Blob>' is not assignable to type 'Blob'.
Type 'HttpSentEvent' is missing the following properties from type 'Blob': size, arrayBuffer, bytes, slice, and 2 more.ts(2322)
Share
Improve this question
edited Feb 3 at 6:06
DarkBee
15.5k8 gold badges72 silver badges118 bronze badges
asked Feb 3 at 4:09
keshav godanikeshav godani
3132 gold badges5 silver badges16 bronze badges
2 Answers
Reset to default 0The reason is evident in the error message
Type 'Observable<HttpEvent>' is not assignable to type 'Observable'. Type 'HttpEvent' is not assignable to type 'Blob'.
HttpClient.get
returns an Observable
by default. You can type-cast it as follows:
protected getBlob(url: string, options: any = {}): Observable<Blob> {
return this.http.get<Blob>(url, { responseType: 'blob' as 'json', ...options });
}
as json
is for typescript type-checking
Due to object destructuring { ...options, responseType: 'blob' }
the typing is lost, just reapply the typing using as
of typescript like shown below.
protected getBlob(url: string, options: any = {}): Observable<Blob> {
return this.http.get(url, { ...options, responseType: 'blob' } as {
responseType: 'blob';
});
}
本文标签: angularHow to handle binary file or XLSX as a responseStack Overflow
版权声明:本文标题:angular - How to handle binary file or XLSX as a response? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745249753a2649750.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论