JS创建a标签下载导出文件,兼容性写法
文章目录
if ('msSaveOrOpenBlob' in navigator) { // 兼容edge
window.navigator.msSaveOrOpenBlob(blob, fileName)
} else if ('download' in document.createElement('a')) { // chrome等
const aLink = document.createElement('a')
aLink.href = URL.createObjectURL(blob)
aLink.setAttribute('download', fileName) // 设置下载文件名称
document.body.appendChild(aLink)
aLink.click()
document.body.removeChild(aLink)
window.URL.revokeObjectURL(aLink.href)
} else { // 兼容IE10+
window.navigator.msSaveBlob(blob, fileName)
}
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论