多种类型文件流下载
const downloadFile=(res, fileName, fileType)=> {
let blob
if (fileType== '.xls') {
blob = new Blob([res], { type: 'application/vnd.ms-excel' })
} else if (fileType == '.xlsx') {
blob = new Blob([res], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
} else if (fileType== '.doc') {
blob = new Blob([res], { type: 'application/msword' })
} else if (fileType== '.docx') {
blob = new Blob([res], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' })
} else if (fileType== '.pdf') {
blob = new Blob([res], { type: 'pplication/pdf' })
} else if (fileType== '.ppt') {
blob = new Blob([res], { type: 'application/vnd.ms-powerpoint' })
} else if (fileType== '.png') {
blob = new Blob([res], { type: 'image/png' })
} else if (fileType== '.pptx') {
blob = new Blob([res], { type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation' })
} else if (fileType== '.jpeg') {
blob = new Blob([res], { type: 'image/jpeg' })
} else if (fileType== '.zip') {
blob = new Blob([res], { type: 'application/zip' })
} else if (fileType== '.7z') {
blob = new Blob([res], { type: 'application/x-7z-compressed' })
} else if (fileType== 'tar') {
blob = new Blob([res], { type: 'application/x-tar' })
} else if (fileType== '.7z') {
blob = new Blob([res], { type: 'application/x-7z-compressed' })
}else{
this.$message.error('该文件类型未配置,请配置文件类型')
return false
}
if (blob) {
let link = document.createElement('a')
link.style.display = 'none'
link.href = window.URL.createObjectURL(blob)
link.download = fileName + fileType
link.click()
URL.revokeObjectURL(elink.href)
document.body.removeChild(elink)
}
}