const btnElement = document.getElementById('btn')
btnElement.addEventListener('click', download)
function download() {
const xhr = new XMLHttpRequest()
const url = document.querySelector('input').value
if(!url) {
alert('url is null')
return
}
xhr.open('GET', url)
xhr.responseType = 'blob'
xhr.send()
xhr.onload = function() {
const fileUrl = URL.createObjectURL(xhr.response)
const a = document.createElement('a')
a.setAttribute('href', fileUrl)
a.setAttribute('download', '')
a.click()
}
}
1. 要求访问地址 允许跨域