1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| import Axios from "axios"; import Vue from "vue";
let exprotUtils = { exportX2(url, fromData, fileName, method = "POST", HOST = "") { let hostUrl = '默认url'if (HOST) { hostUrl = HOST; } return Axios(hostUrl + url, { method: method, headers: { Authorization: "bearer " + localStorage.getItem("token"), "client-type": "browser", "Content-Type": "application/json;", Accept: "application/json" }, data: { ...fromData }, responseType: "blob" }).then(res => { if (res && res.message) { return new Promise((res, rej) => { res(false); }); } if (res && res.status == "200") { return exprotUtils._downloadExcel(res, fileName); } }); }, _downloadExcel(res, name = "导出.xlsx") { return new Promise((resolve, reject) => { if (res != null) { const content = res.data; const fileName = name; const blob = new Blob([content], { type: "application/ms-excel" }); if ("download" in document.createElement("a")) { 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); document.body.removeChild(elink); } else { navigator.msSaveBlob(blob, fileName); } resolve(true); } else { reject(false); } }); } }; export default exprotUtils;
|