当我们下载文件时,请求服务器的文件会在浏览器打开这个链接,下面代码就是直接下载文件不预览的方法
axios({
method: "get",
url: 你的url,
responseType: "blob",
}).then((res) => {
let hzIndex=res.config.url.lastIndexOf('.');//获取点的位置
let lineIndex=res.config.url.lastIndexOf('\\');//获取划线的位置
let name=res.config.url.slice(lineIndex+1,hzIndex);//获取文件名
const href = URL.createObjectURL(res.data);
const a = document.createElement("a");
a.setAttribute("href", href);
a.setAttribute("download",name);
a.click();
URL.revokeObjectURL(href);
});
}