这里调接口时记得加上responseType: 'blob'
/**
* @description: 下载文件
* @param {string} data 文件流
* @param {string} filename 文件名
* @return {*}
*/
export function downloadFile(data: string, filename: string) {
const blob = new Blob([data])
const url = window.URL.createObjectURL(blob) // pdf有url的可直接使用url
const link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('id', 'pdfID')
link.setAttribute('download', filename)
document.body.appendChild(link)
link.click()
setTimeout(() => {
document.body.removeChild(link)
window.URL.revokeObjectURL(url)
}, 0)
}
标签:const,string,自定义,url,导出,js,link,blob,filename
From: https://www.cnblogs.com/haPro/p/17328891.html