在api文件中的接口定义:
export function downLoadFile (q) {
return axios({
url: '/file/downLoadFile',
method: 'get',
params: q,
responseType: 'blob'
})
}
下载按钮的调用方法:
down(id) {
downLoadFile({id}).then(res => {
let blob = new Blob([res],{type: 'application/pdf;charset=utf-8'});
let downloadElement = document.createElement('a');
let href = window.URL.createObjectURL(blob); //创建下载的链接
downloadElement.href = href;
// downloadElement.setAttribute('download', 'fileName')
document.body.appendChild(downloadElement);
downloadElement.click(); //点击下载
document.body.removeChild(downloadElement); //下载完成移除元素
window.URL.revokeObjectURL(href); //释放掉blob对象
})
},
本文介绍如何在Vue应用中通过接口请求实现文件下载。主要涉及Vue调用API接口,以及处理下载文件的方法。
481

被折叠的 条评论
为什么被折叠?



