文件流下載
后端返回文件流形式,前端下載
// res 為請求返回的數據對象const file_data = res.data // 后端返回的文件流const blob = new Blob([file_data])
const href = window.URL.createObjectURL(blob) // 創建下載的鏈接
const file_name = decodeURI(res.headers['content-disposition'].replace('attachment;filename=', ''))
console.log(file_name) // 從請求頭獲取文件名
const downloadElement = document.createElement('a')
downloadElement.style.display = 'none'
downloadElement.href = href
downloadElement.download = file_name // 下載后文件名
document.body.appendChild(downloadElement)
downloadElement.click()
document.body.removeChild(downloadElement) // 下載完成移除元素
window.URL.revokeObjectURL(href) // 釋放掉blob對象
?base64格式下載
后端返回base64格式,前端下載
// res 后端返回的文件base64const link = document.createElement('a')
link.href = 'data:application/octet-stream;base64,' + res
link.download = '下載的文件名'
link.click()