1、点击事件 index.vue
<el-link type="primary"
:underline="false"
style="font-size:12px;vertical-align: baseline;"
@click="importTemplate"
>下载模板</el-link>
2、处理方法 index.vue
import { exportTemplate } from "@/xxxxxxxx";
import { downloadFile } from "@/utils/index";
importTemplate() {
exportTemplate().then(res => {
downloadFile(res, "客户导入模板", "xlsx");
});
},
3、处理流对象的方法 utils/index.js
export function downloadFile(obj, name, suffix) {
const fileName = parseTime(new Date()) + "-" + name + "." + suffix;
if (window.navigator.msSaveBlob) {
try {
window.navigator.msSaveBlob(new Blob([obj]), fileName);
} catch (e) {
console.log(e);
}
} else {
const link = document.createElement("a");
let blob = new Blob([obj], {
type: "application/vnd.ms-excel"
});
link.style.display = "none";
link.href = URL.createObjectURL(blob);
link.setAttribute("download", fileName);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}