前端开发小技巧
import axios from "axios";
import store from "../store";
import { Message, MessageBox } from "element-ui";
// post导出
export default function exportMethod(url, data) {
axios
.post(url, data, {
headers: {
Authorization: store.getters.token
},
responseType: "blob"
})
.then(res => {
let content = res.data;
const blob = new Blob([content], { type: "application/ms-excel" });
let fileName = res.headers["content-disposition"].split("=")[1];
/*let Base64 = require('js-base64').Base64;
fileName = Base64.decode(fileName)*/
fileName = decodeURIComponent(fileName);
if ("download" in document.createElement("a")) {
// 非IE下载
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);
} else {
// IE10+下载
navigator.msSaveBlob(blob, fileName);
}
})
.catch(function(error) {
MessageBox.confirm(
"提示",
"下载失败",
{
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning"
}
).then(() => {
});
});
}
import exportData from "utils/exportData.js";//引入
// 事件函数(query形式)
exportData(row) {
console.log(row);
exportData("/api/sampling/test/export?sampleId=" + row.sampleId, {
// sampleId: row.sampleId
});
},
//事件函数(data形式)
handleExport() {
exportData("/api/sampling/person/export", this.listQuery);
},