在最近项目中遇到一个下载文件的场景,我的第一想法是用Windows.pen()方法来下载文件,
后来发现Windows.pen()只能下载XLSX表格和word文档
项目需求是所有文件所以Windows.pen()方法是不可行的, 于是又想到 a标签中 有一个download 方法,
downloadFile_1(fileName, filePath) {
if (!filePath) {
return
}
// let url = window.URL.createObjectURL(new Blob([filePath]))
let link = document.createElement('a')
link.style.display = 'none'
link.href = filePath
link.setAttribute('download', `${fileName}.pdf`)
document.body.appendChild(link)
link.click()
},
在百度淘到的方法,在我看来太麻烦了,于是就想这种常用的方法肯定有人封装包吧, 于是在百度查找就有了方向, 不负众望 , 还真被我找到了
GitHub:github.com/rndme/downl…
vue使用方法: 首先命令行
npm install downloadjs -S
在需要使用的页面xxx.vue文件的script标签内
import download from 'downloadjs'
在你需要下载的方法内
downloadPdf(){
download('xxx.com/xxxx.pdf')
},