- 经常出现点击pdf文件直接跳转到预览,以下是实现不跳转,直接下载文件的方法
- 使用axios之前请求pdf路径,转换成流文件,然后可以直接进行进行下载,就不会直接打开pdf文件了
axios({
method: 'get',
url: 'pdf文件.pdf',
responseType: 'blob'
}).then((res) => {
this.downloadFile(res.data, "文件", ".pdf");
})
downloadFile(obj, name, suffix) {
const url = window.URL.createObjectURL(new Blob([obj]));
const link = document.createElement("a");
link.style.display = "none";
link.href = url;
const fileName = parseTime(new Date()) + "-" + name + "." + suffix;
link.setAttribute("download", fileName);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}