第一种方法
const downloadfile = (url) => {if (!url) {return ElMessage.error("暂无文件!无法下载")}axios({url,method: 'GET',responseType: 'blob'// headers: {// token:getCache('TOKEN'), // 可以携带token// }}).then(res => {const href = URL.createObjectURL(res.data);const a = document.createElement('a');a.download = '名字.pdf';//后缀改为对应的文件类型a.href = href;a.click();setTimeout(() => {a.remove();}, 100);});
}
第二种方法
//response为文件流//不同类型的文件用不同类型的typeconst blob = new Blob([response], {type: "application/msword",});const fileName = "名字.xlsx";//后缀改为对应的文件类型const elink = document.createElement("a");elink.download = fileName;elink.style.display = "none";elink.href = URL.createObjectURL(blob);document.body.appendChild(elink);elink.click();URL.revokeObjectURL(elink.href); // 释放URL 对象document.body.removeChild(elink);