下载 Blob
Blob 就是前端的二进制文件. 下载是利用浏览器的 a 标签, 触发点击行为来实现下载:
axios 的请求
axios.post('...', {
responseType: 'blob'
})
前端代码参考:
const myDate = new Date();
const fileName = `主数据${myDate.toLocaleDateString()}.xlsx`;
if (window.navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, fileName);
} else {
const link = document.createElement('a');
const evt = document.createEvent('MouseEvents');
link.style.display = 'none';
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
document.body.appendChild(link); // 此写法兼容可火狐浏览器
evt.initEvent('click', false, false); // 触发事件
link.dispatchEvent(evt);
document.body.removeChild(link); // 移除该标签
}