const link = document.createElement('a')
link.style.display = 'none'
link.download = url.substring(url.lastIndexOf('/') + 1)
link.href = url
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
有的是打开链接预览,没有直接下载。就需要用这个方法
var x = new XMLHttpRequest()
x.open('GET', url, true)
x.responseType = 'blob'
x.onload = function () {
var href = window.URL.createObjectURL(x.response)
const link = document.createElement('a')
link.style.display = 'none'
link.download = href.substring(href.lastIndexOf('/') + 1)
link.href = href
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
}
x.send()
})
.catch(() => {
})