利用axios的post方式实现文件的下载
程序员文章站
2022-10-03 17:08:12
1、前端代码:dowloadTest(){ this.$axios.request({ method:'post', url:'/file/download', data:{ fileid:123 }, responseType:'arraybuffer' }) .then((response)=>{ this.download(response.data); })}, download (data) {...
1、前端代码:
dowloadTest(){
this.$axios.request({
method:'post',
url:'/file/download',
data:{
fileid:123
},
responseType:'arraybuffer'
})
.then((response)=>{
this.download(response.data);
})
},
download (data) {
if (!data) {
return
}
let url = window.URL.createObjectURL(new Blob([data]))
let link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.download = 'ea7c0cf24153e0cd62bc8b64841fd84d.jpg'; //下载后文件名
link.setAttribute('download', 'ea7c0cf24153e0cd62bc8b64841fd84d.jpg')
document.body.appendChild(link)
link.click()
}
2、后端: 返回文件流即可 !!!
本文地址:https://blog.csdn.net/JavaWebEngineer/article/details/107410937
上一篇: Java基础-流程控制
下一篇: 批量删除和修改文件名前缀