我这里是一个pdf文件,在页面按钮点击后希望实现文件下载功能,而不是在页面打开pdf预览
有一个插件很好https://github.com/kennethjiang/js-file-download
我们在vue-cli的依赖中找到它,并安装
在src/utils/filedownload.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import axios from 'axios' import fileDownload from 'js-file-download'; export function download(url, filename) { //因为跨域,去掉域名,使用vue.config.js代理转发 url = '/' + url.split('/').slice(3).join('/') axios.get('/api/'+url, { responseType: 'blob', withCredentials: true, crossDomain: true, }).then(res => { fileDownload(res.data, filename); }); } |
如何使用呢?
1 |
download(fileurl,filename+'.pdf'); |