本文提供实现pdf,zip等文件下载方式
一:pdf,zip等文件下载:
1.请求封装
export function downloadFj(params) {
return http.post('/api/1/fmis/statBillfileUnzip/downloadFj', params, {
responseType: 'blob',
})
}
2.调用
async downloadFile() {
let res = await downloadFj(参数)
if (res.status === 200 && res.data) {
const { data } = res
const blob = new Blob([data])
let disposition = decodeURI(res.headers['content-disposition'])
// 从响应头中获取文件名称
let fileName = disposition.substring(disposition.indexOf('filename=') + 9, disposition.length)
if ('download' in document.createElement('a')) {
// 非IE下载
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) // 释放URL 对象
document.body.removeChild(elink)
} else {
// IE10+下载
navigator.msSaveBlob(blob, fileName)
}
} else {
this.$message.error('下载失败')
}
},