非同源url文件下载

// 封装一个download方法
const downloadExcelFile = (path, name) => {
const x = new window.XMLHttpRequest();
x.open('GET', path, true);
x.responseType = 'blob';
x.onload = () => {
const url = window.URL.createObjectURL(x.response);
const a = document.createElement('a');
a.href = url; // 下载链接
a.download = name + '.xlsx';  // 文件名
a.click();
};
x.send();
};

posted @ 2023-02-08 10:02  吃饭七分饱  阅读(51)  评论(0编辑  收藏  举报