a标签实现下载文件功能
a标签实现下载文件功能
·
一、基本用法
<a href='http://192.168.1.1/eee.xlsx' download='eee.xlsx'>下载</a>
注意:这种写法有个前提:href 的下载地址和当前网站地址必须是同源的,否则 download 不生效。
二、download 不生效时解决方案,具体代码如下:
// 下载文件
downloadFile() {
const xhr = new window.XMLHttpRequest()
xhr.open('GET', 'http://192.168.1.1/eee.xlsx', true)
xhr.responseType = 'blob'
xhr.onload = () => {
const url = window.URL.createObjectURL(xhr.response)
const link = document.createElement('a')
link.href = url
link.download = 'eee.xlsx'
link.click()
}
xhr.send()
}
更多推荐
所有评论(0)