现在的代码
/*
拿到并储存文件后缀
*/
response hoader {
content-disposition: attachment;filename=xxx.xlsx
}
const disposition = response.headers['content-disposition']
const suffix = disposition.split('.').slice(-1)[0]
Vue.ls.set('DOWNLOAD_FILE_NAME', `.${suffix}`)
下面我准备用拿到的suffix
匹配MIME
类型
/*
模拟点击a标签效果
*/
const suffix = Vue.ls.get('DOWNLOAD_FILE_NAME')
const blob = new Blob([value], {
type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
})
const url = window.URL.createObjectURL(blob)
const link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', ${fileName})
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
这样挺好了,不然后端干嘛要传流,直接文件链接不是更好?