const url = '/download/possible'
let downData = {
'param':JSON.stringify(selectData)
}
const options = {
method: 'POST',
headers: { 'content-type': 'application/x-www-form-urlencoded' },
data: qs.stringify(downData),
responseType: 'arraybuffer',
url
}
axios(options).then((res) => {
let data =res.data
let blob = new Blob([data],{type: 'text/plain;charset=UTF-8'})
let url = window.URL.createObjectURL(blob)
let link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', '文件.docx')
document.body.appendChild(link)
link.click()
// 然后移除
document.body.removeChild(link)
// link.addEventListener('click', e => {
// e.stopPropagation()
// // 点击以后移除link节点
// link.parentNode.removeChild(link)
// })
// link.click()
// console.log(res)
})
刚开始的时候下载成功后文件不能打开,后来搜索出来,加上responseType: ‘arraybuffer’, 还有{type: ‘text/plain;charset=UTF-8’}
才可以正常打开文件