<button nz-button nzType="default" (click)="downTemplate()">
<i nz-icon nzType="download"></i>下载模板
</button>
downTemplate=()=>{
const params = {}; // body的参数
this.httpClient.post('/file/down/template?type=3', params, {
responseType: "blob",
headers: new HttpHeaders().append("Content-Type", "application/json")
}).subscribe(resp=>{
// resp: 文件流
this.downloadFile(resp);
})
}
/**
* 创建blob对象,并利用浏览器打开url进行下载
* @param data 文件流数据
*/
downloadFile(data) {
// 下载类型 xls
const contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
// 下载类型:csv
const contentType2 = 'text/csv';
const blob = new Blob([data], { type: contentType });
const url = window.URL.createObjectURL(blob);
// 打开新窗口方式进行下载
// window.open(url);
// 以动态创建a标签进行下载
const a = document.createElement('a');
const fileName = "模板下载";
a.href = url;
// a.download = fileName;
a.download = fileName + '.xlsx';
a.click();
window.URL.revokeObjectURL(url);
}