ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

前端文件下载方法

2022-08-08 10:03:56  阅读:95  来源: 互联网

标签:case 文件 code 前端 fileName break application link 下载


function switchMime(fileName) {
let suffix = fileName.split('.')[fileName.split('.').length - 1]
let code = 'application/msword'
switch (suffix) {
case 'doc':
code = 'application/msword'
break
case 'docx':
code =
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
break
case 'pdf':
code = 'application/pdf'
break
case 'ppt':
code = 'application/vnd.ms-powerpoint'
break
case 'pptx':
code =
'application/vnd.openxmlformats-officedocument.presentationml.presentation'
break
case 'rar':
code = 'application/x-rar-compressed'
break
case 'xls':
code = 'application/vnd.ms-excel'
break
case 'xlsx':
code = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
break
case 'zip':
code = 'application/zip'
break
case 'txt':
code = 'text/plain'
break
default:
break
}
return code
}
this.axios({
method: 'post',
url: '/aeValueSetsEdit/download2',
responseType: 'blob'
}).then(function(res) {
let temp = res.headers['content-disposition']
.split(';')[1]
.split('filename=')[1] //从返回头取出文件名称
let fileNameStr = unescape(decodeURI(temp)) //中文文件名需解码
let fileName = fileNameStr.replace(/\"/g, '').replace(/\'/g, '')
let blob = new Blob([res.data], {
type: switchMime(fileName)
})
if (window.navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, fileName)
} else {
var link = document.createElement('a')
document.body.appendChild(link)
link.href = window.URL.createObjectURL(blob)
link.download = fileName
link.click()
//释放内存
window.URL.revokeObjectURL(link.href)
document.body.removeChild(link)
}
})

标签:case,文件,code,前端,fileName,break,application,link,下载
来源: https://www.cnblogs.com/zmdbk/p/16560753.html

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有