流文件下载方法
const download = (res, mime) => {
const mimeList = {
pdf: 'application/pdf',
jpg: 'image/jpeg',
jpeg: 'image/jpeg',
gif: 'image/gif',
png: 'image/png',
doc: 'application/msword',
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
ppt: 'application/vnd.ms-powerpoint',
pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
xls: 'application/vnd.ms-excel',
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
zip: 'application/zip',
rar: 'application/x-rar-compressed',
bmp: 'image/bmp',
tif: 'image/tiff',
}
if (res && res.type.indexOf('application/json') > -1) {
const reader = new FileReader()
reader.onload = () => {
if (reader.result) {
const result = JSON.parse(reader.result)
Vue.prototype.$message.error(result.desc)
}
}
reader.readAsText(res, 'utf-8')
return
}
const blob = new Blob([res], { type: mimeList[mime] || 'application/vnd.ms-excel' })
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blob, downloadName)
} else {
const blobURL = window.URL.createObjectURL(blob)
const tempLink = document.createElement('a')
tempLink.style.display = 'none'
tempLink.href = blobURL
tempLink.setAttribute('download', downloadName)
if (typeof tempLink.download === 'undefined') {
tempLink.setAttribute('target', '_blank')
}
document.body.appendChild(tempLink)
tempLink.click()
document.body.removeChild(tempLink)
setTimeout(() => {
window.URL.revokeObjectURL(blobURL)
}, 50)
}
}