使用axios.post请求下载,后台返回文件流数据。前端需要将文件流转化一下
首先在post请求时要加 responseType: ‘blob’
this.$axios.post(this.$url,{"keyword":this.select_name},{responseType: 'blob'})
对返回的数据流做处理,blob的type是要转换的文件类型,这里我的是pdf。可参考https://www.cnblogs.com/yjmBlogs/p/9493726.html
let blob = new Blob([res.data], {type: "application/pdf"}); // res就是接口返回的文件流了
let objectUrl = URL.createObjectURL(blob);
let link = document.createElement('a')
link.style.display = 'none'
link.href = objectUrl
link.setAttribute('download', 'pdf.pdf')
document.body.appendChild(link)
link.click()