<span class="a-down" @click="a_down(data_address,'模板.xlsx')">下載</span>
//接口請(qǐng)求下載地址
created() {
forecastDownload({}).then((res) => {
this.data_address = res.data;
});
},
methods: {
getBlob(url) {
return new Promise((resolve) => {
const xhr = new XMLHttpRequest()
xhr.open('GET', url, true)
xhr.responseType = 'blob'
xhr.onload = () => {
if (xhr.status === 200) {
resolve(xhr.response)
}
}
xhr.send()
})
}
,
// 自定義下載文件名稱(chēng)
saveAs(blob, filename) {
if (window.navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, filename)
} else {
const link = document.createElement('a')
const body = document.querySelector('body')
link.href = window.URL.createObjectURL(blob) // 創(chuàng)建對(duì)象url
link.download = filename
// fix Firefox
link.style.display = 'none'
body.appendChild(link)
link.click()
body.removeChild(link)
window.URL.revokeObjectURL(link.href) // 通過(guò)調(diào)用 URL.createObjectURL() 創(chuàng)建的 URL 對(duì)象
}
},
// 點(diǎn)擊下載按鈕
a_down(url, name) {
this.getBlob(url).then((blob) => {
this.saveAs(blob, name)
})
}