这里直接贴代码吧,也没什么好说的
this.outQueryUrl = 'localhost' // 请求地址
const data = JSON.stringify(this.listQuery) //请求参数,需要转成json字符串
// ajax请求实例
const ajax = new XMLHttpRequest()
const token = getToken()
ajax.onreadystatechange = function() {
if (ajax.readyState === 4 && ajax.status === 200) {
var blob = this.response
const result = window.URL.createObjectURL(new Blob([blob]))
const link = document.createElement('a')
link.href = result
link.setAttribute('download', '资产清单.xls') // 这里设置文件格式
document.body.appendChild(link)
link.click()
}
}
ajax.open('post', this.outQueryUrl, true)
ajax.setRequestHeader('token', token) // 如果需要 token 校验
ajax.setRequestHeader('Content-type', 'application/json;charset=UTF-8')
ajax.setRequestHeader('Accept', 'application/json, text/plain, */*')
ajax.responseType = 'blob'
ajax.send(data)
网友评论