HTML代码
我这里用的是a标签进行下载
JS代码
function download(item) {
let fileName = item.filename
downloadFile({ id: item.id }).then((res) => {
let downloadData = res.data //后端接口返回的文件流 就是那一堆乱码
fileDownload(downloadData, fileName)
})
}
通用代码
function fileDownload(obj, fileName) {
const url = window.URL.createObjectURL(new Blob([obj]))
const link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', fileName)
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
}
接口里记得加上responseType: 'blob', 不然下载下来还是一堆乱码