实现思路:通过Http封装的请求方法,对参数格式进行格式匹配
首先得了解Http封装的Post方法,传入对应的三个参数url,body,options,具体代码如下:
post(url: string, body: any | null, options: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
context?: HttpContext;
observe?: 'body';
params?: HttpParams | {
[param: string]: string | number | boolean | ReadonlyArray<string | number | boolean>;
};
reportProgress?: boolean;
responseType: 'blob';
withCredentials?: boolean;
}): Observable<Blob>;
对应的Service调用Post方法,选择文件类型{responseType: ‘blob’}:
DownLoadExcel() {
this.http.post(
this.baseUrl + '/prf/download_file', null, {
responseType: 'blob' })
.toPromise()
.then((res) => {
const blob = new Blob([res], {
type: 'application/vnd.ms.excel' }); //处理返回的数据
const file = new File([blob], 'template' + '.xlsx', {
type: 'application/vnd.ms.excel',
});
saveAs(file);
});
}
若有其他地方使用,调用该Service中的DownLoadExcel()即可