a标签下载问题

261 阅读1分钟
		<a href="../text.jpg" download=""123>下载资料</a>

同源情况下download 属性可正常进行

非同源情况下,需要借用浏览器的bobl属性

 const downloadRes = async (url, name) => {
let response = await fetch(url)
// 内容转变成blob地址
let blob = await response.blob()
// 创建隐藏的可下载链接
let objectUrl = window.URL.createObjectURL(blob)
let a = document.createElement('a')
//地址
a.href = objectUrl
//修改文件名
a.download = name
// 触发点击
document.body.appendChild(a)
a.click()
//移除
setTimeout(() => document.body.removeChild(a), 1000)
}

#弊端:: 下载完成后才会弹出提示