方式一: 使用封装的Request工具实现文件流下载(常用的一种)
- 封装后的Request工具调用简单,代码更简洁
- 支持传递Token进行身份验证,安全性较高
const onExport = () => {
setExportLoading(true);
const params = filterRef.current;
wapper(overviewResa.downloadUrl(params)).then(response => {
setExportLoading(false);
const blob = new Blob([response], { type: 'application/octet-stream' });
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = '模型概览.xlsx';
document.body.appendChild(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
});
};
方式二: 直接通过URL跳转下载
- 实现简单,不需要处理Blob对象及下载链接
- 适合下载无需身份验证或参数简单的文件
const onDownLoadExcel = () => {
let baseURL=`${window.location.protocol}//${process.conf.ip}${process.conf.nginx ? '/gate11' : ''}`;
window.open(baseURL + '/asset/xxx/download?templete=设备树类型导入模板');
}
};