vue项目,后端返文件流来下载
封装通用方法:
export function download(url, params, filename, type, contentType) {
return service.post(url, params, {
headers:{ 'Content-Type': contentType},
responseType: 'blob'
}).then((data) => {
const content = data
let blob = null
if (type) {
blob = new Blob([content], {type})
} else {
blob = new Blob([content])
}
if ('download' in document.createElement('a')) {
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)
document.body.removeChild(elink)
} else {
navigator.msSaveBlob(blob, filename)
}
}).catch((r) => {
console.error(r)
})
}
- 导出zip格式
idList: 数组id
download(url,idList, `某某.zip`,'application/zip','application/json; application/octet-stream').then( res => {
this.loading = false
this.ids = []
// this.$refs.multipleTable.clearSelection()
})
- 导出excel
download(url,idList, `某某.excel`,null,'application/x-www-form-urlencoded').then( res => {})