最近用vue整前端,有个下载帮助文档的需求,前后端分离的项目,对于万年不变的帮助文档来说,本次就准备放在前端,实现直接从前端下载文件:
下面是实现的具体方法:
downfile() {
var url = "../static/file/test.pdf";
var filename = "test.pdf";
return fetch(url).then(res => res.blob().then(blob => {
let a = document.createElement('a');
let url = window.URL.createObjectURL(blob);
a.href = url;
a.download = filename;
a.click();
window.URL.revokeObjectURL(url);
}))
}