fetch(url, {
method: 'get',
responseType: 'blob',
headers: {
Authorization: 'Bearer ' + store.getters.token,
},
})
.then(res => {
// 获取响应头
const fileNameEncode = res.headers
.get('Content.Disposition')
.split('filename=')[1]
fileName = decodeURIComponent(fileNameEncode)
return res.blob()
})
.then(res => {
const url = URL.createObjectURL(res)
const aLink = document.createElement('a')
aLink.href = url
aLink.setAttribute('download', fileName)
document.body.appendChild(aLink)
aLink.click()
document.body.removeChild(aLink)
})
使用fetch请求,如何获取响应头Content-Disposition中的文件名
于 2023-09-26 16:51:29 首次发布