ts语法封装一个下载下载文件的函数
// Internet Explorer 10 的 msSaveBlob 和 msSaveOrOpenBlob
// 方法允许用户在客户端上保存文件,方法如同从 Internet 下载文件
// ,这是此类文件保存到“下载”文件夹的原因。
export function downloadFile (blob: Blob, filename: string) {
if ('msSaveOrOpenBlob' in navigator) {
window.navigator.msSaveOrOpenBlob(blob, filename)
return
}
const url = window.URL.createObjectURL(blob)
const link = document.createElement('a')
link.download = filename
link.style.display = 'none'
link.href = url
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
window.URL.revokeObjectURL(url)
}