1.后台返回二进制流
前端通过get请求访问后台,后台返回二进制流
axios({
method: "get",
url: '接口地址',
headers: {
"content-type": "application/json; charset=utf-8",
},
responseType: "blob",
}).then(function (res) {
const blob = res.data
const link = document.createElement('a')
// 创建下载的链接
const href = window.URL.createObjectURL(blob)
link.href = href
// 下载后文件名
link.download = decodeURI('图片.png')
document.body.appendChild(link)
link.click()
link.remove()
})
2.后台返回的是一个地址(链接)
前端通过get请求拿到的是一个文件链接
axios({
method: "get",
url: '接口地址',
}).then(function (res) {
let link = document.createElement('a')
link.href = res.data.url
link.setAttribute('download', '文件名')
document.body.appendChild(link)
link.click()
link.remove()
})