下载文件-get方式
import axios from "axios"; export const axiosDownload = async (url, params, fileName) => { let res = await axios.get(url, { responseType: 'blob', headers: { 'content-type': 'application/json; charset=utf-8' }, params: params, }); let blob = new Blob([res.data]); let downloadElement = document.createElement('a'); let href = window.URL.createObjectURL(blob); //创建下载的链接 downloadElement.href = href; downloadElement.download = fileName; //下载后文件名 document.body.appendChild(downloadElement); downloadElement.click(); //点击下载 document.body.removeChild(downloadElement); //下载完成移除元素 window.URL.revokeObjectURL(href); //释放掉blob对象 };
export const axiosDownload = async (url, params, fileName) => { const hide = message.loading('正在加载中,请耐心等待', 3); let res = await axios.get(url + '?random=' + Math.random(), { responseType: 'blob', headers: {'content-type': 'application/json; charset=utf-8'}, params: params, }); let headers = res.headers; if (headers.suffix && headers.fileName) { fileName = decodeURI(headers.fileName, "utf-8") + headers.suffix; } console.log("headers", headers); // let blob = new Blob([res.data], { type: 'application/octet-stream', 'Content-Disposition': 'attachment' }); if (window.navigator && window.navigator.msSaveOrOpenBlob) { // 兼容ie浏览器 window.navigator.msSaveOrOpenBlob(blob, fileName) } else { // 谷歌,火狐等浏览器 let url = window.URL.createObjectURL(blob) let downloadElement = document.createElement('a') downloadElement.style.display = 'none' downloadElement.href = url downloadElement.download = fileName document.body.appendChild(downloadElement) downloadElement.click() document.body.removeChild(downloadElement) window.URL.revokeObjectURL(url) } hide(); message.success("已加入下载队列") };