js 页面下载3中方式

1. window.location.href = '请求路径'get方式
}


2. fetch('请求路径方式拼接上去'get方式).then(res => res.blob().then(blob => {
const a = document.createElement('a')
const url = window.URL.createObjectURL(blob)
const filename = 'aa.xlsx'
a.href = url
a.download = filename
a.click()
window.URL.revokeObjectURL(url)
}))


3.this.exportData.campusId = this.campusId
this.exportData.time = this.changeTimes(this.createEndTime)
axios({
url:'路径'
method: '方式',
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
data: 参数,
responseType: 'blob',
timeout: 120000
}).then(response => {
const blob = new Blob([response.data])
const fileName ='aa.xlsx'
if ('download' in document.createElement('a')) { // 非IE下载
const elink = document.createElement('a')
elink.download = fileName
elink.style.display = 'none'
elink.href = URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href) // 释放URL 对象
document.body.removeChild(elink)
} else { // IE10+下载
navigator.msSaveBlob(blob, fileName)
}
posted @ 2019-09-24 12:13  历尽千帆归来任是少年  阅读(810)  评论(0编辑  收藏  举报