post实现下载/导出功能
程序员文章站
2024-03-20 16:13:04
...
post实现下载/导出功能
话不多说 直接上码
downLoad (row) {
let params = new FormData()
params.append('xxx', '1')
params.append('yyy', '2')
const data = await this.$http.post('', params, {responseType: 'blob'})
let blob = new Blob([data.data], { type: 'application/zip' })
if (window.navigator.msSaveOrOpenBlob) {
// 兼容ie
navigator.msSaveBlob(blob)
// navigator.msSaveBlob(blob,'xxx.xx') //可自定义文件名
} else {
let objectUrl = URL.createObjectURL(blob)
let downLoadLink = document.createElement('a')
downLoadLink.href = objectUrl
// downLoadLink.download = '' // 可自定义文件名
document.body.appendChild(downLoadLink)
downLoadLink.click()
document.body.removeChild(downLoadLink)
window.URL.revokeObjectURL(downLoadLink.href)
}
}