跳到主要内容

1 篇博文 含有标签「工具函数」

查看所有标签

常用下载函数

· 阅读需 1 分钟
TransonQ
Front-end Engineer

链接下载​

export function downLoadFile(url: string, filename: string, blank = true) {
if (!isURL(url)) return
let downloadLink = document.createElement('a')
downloadLink.download = filename
downloadLink.href = url
blank && (downloadLink.target = '_blank')
downloadLink.click()
window.URL.revokeObjectURL(downloadLink.href)
}

blob 下载​

需要设置 responseType: 'blob'

axios 案例

export const postDownloadLetter = () =>
axios.request({
method: 'post',
url: 'url',
responseType: 'blob',
})

代码

export const downloadBlob = (blob: any, fileName: string) => {
const elink = document.createElement('a')
elink.download = fileName
elink.style.display = 'none'
elink.href = URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href)
document.body.removeChild(elink)
}