在前端下载东西的时候, 有些情况下后端 会提供二进制数据给我们, 我们前端应该如何处理呢?
一 , 在请求头加上 我们要的返回 类型 blob
{ responseType: 'blob', observe: 'response' }
二,前端 拿到blob 数据 后怎么处理
/** 下载模板 */
downloadTemplate() {
this.backendService.downloadWorkflowTemplate().subscribe(data => { // 这里可以忽略只是拿到blob 数据
const link = document.createElement('a');
const blob = new Blob([data.body], { type: 'application/zip' });
// 后面是用 a 标签 的 download 去下载
link.setAttribute('href', window.URL.createObjectURL(blob));
link.setAttribute('download', '模板.xlsx'); // 这里是文件名字和后缀
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
});
}