下载模板(word,pdf,excel)
正文链接
1. 引入请求插件 例子: axios
import axios from "axios"
2. 要素 (url、filename)
let url = "https://****/wallet/8215708557865148.xlsx";
let filename = '模板.xlsx'
this.downFile(url, filename);
3. 发起请求, 二进制流下载文件
async downFile(url, name) {
let res = await axios({
method: "get",
url,
responseType: "arraybuffer"
});
let type = res.headers["content-type"];
this.downs(res, type, name);
}
4. 实现下载
downs(res, type, filename) {
const blob = new Blob([res.data], {
type
});
const a = document.createElement("a");
const URL = window.URL || window.webkitURL;
const herf = URL.createObjectURL(blob);
a.href = herf;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
window.URL.revokeObjectURL(herf);
}