import axios from 'axios' import fa from 'element-ui/src/locale/lang/fa' /** * 下载附件 */ export function downFile(url, fileName) { let type = fileType(url); console.log(type); axios({ url: url, // 服务器上pdf路径 method: 'get', responseType: 'blob' }).then(res => { //console.log(res) let blob = new Blob([res.data], { //type类型后端返回来的数据中会有,根据自己实际进行修改 // 表格下载为 application/xlsx,压缩包为 application/zip等, type: fileType(url), }); if (typeof window.navigator.msSaveBlob !== "undefined") { window.navigator.msSaveBlob(blob, fileName); } else { var blobURL = window.URL.createObjectURL(blob); // 创建隐藏标签进行下载 var tempLink = document.createElement("a"); tempLink.style.display = "none"; tempLink.href = blobURL; tempLink.setAttribute("download", fileName); if (typeof tempLink.download === "undefined") { tempLink.setAttribute("target", "_blank"); } document.body.appendChild(tempLink); tempLink.click(); document.body.removeChild(tempLink); window.URL.revokeObjectURL(blobURL); } }).catch(err => { //loading.close(); // eslint-disable-next-line no-undef console.log(err) }) } export function fileType(url) { let suffix = url.substring(url.lastIndexOf('.')+1) let type; switch (suffix) { case "pdf": type = "application/pdf"; break; case "doc": type = "application/msword"; break; case "docx": type = "application/msword"; break; case "xls": type = "application/vnd.ms-excel"; break; case "xlsx": type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; break; case "ppt": type = "application/vnd.ms-powerpoint"; break; case "pptx": type = "application/vnd.ms-powerpoint"; break; default: type = ""; break; } return type; }