首先,编写一个含有触发事件(@click,@change等甚至created函数里)
//本人使用了ElementUI
<el-button @click="download">下载模版</el-button>
如图所示,现在对其编辑点击事件
//放到methods里面
async download() {
//笔者这边需要先访问后端拿到文件Url地址,如果你也是,将getDownloadUrl换成你自己的,无需则跳过。
getDownloadUrl()是你在request等js里面定义的请求
const res = await getDownloadUrl();
console.log("模版", res);
//自己看F12里面你的文件地址返回的是什么,有的人是res.data。有的不一样,尾缀一般是***.xlsx或者别的。
//将线上环境于拿到的文件地址进行拼接,不需要的话就直接用
const Url ="http://123.**.**.**:**/" +res.data.url
console.log(Url, "Url");
//此处最优解为实用promise。笔者省事直接用了个定时器
setTimeout(() => {
window.open(Url);
}, 500);
},
如此就可以下载模版