href的属性地址和前端同源情况下
downLoadFile (fileData) {
const a = document.createElement('a')
a.style.display = 'none'
a.setAttribute('target', '_blank')
fileData.name && a.setAttribute('download', fileData.name)
fileData.name && a.setAttribute('download', decodeURIComponent(fileData.name))
a.href = fileData.url
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
}
href的属性地址和前端不同源情况下
downLoadFile (fileData) {
const x = new window.XMLHttpRequest();
x.open('GET', fileData.url, true)
x.responseType = 'blob'
x.onload = () => {
const url = window.URL.createObjectURL(x.response)
const a = document.createElement('a')
a.href = url
a.download = fileData.name
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
}
x.send()
}