export function downLoadFn(data, name) {
const str = name.substring(name.lastIndexOf(".") + 1)// 截取获取图片类型
let ContentType = ""
switch (str) { //在这里添加要下载文件的ContentType
case "jpeg":
ContentType = "image/jpeg"
break
case "jpg":
ContentType = "image/jpeg"
break
case "gif":
ContentType = "image/gif"
break
case "jfif":
ContentType = "image/jpeg"
break
case "png":
ContentType = "application/x-png"
break
case "doc":
ContentType = "application/msword"
break
case "docx":
ContentType =
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
break
case "pdf":
ContentType = "application/octet-stream"
break
case "csv":
ContentType = "text/csv,charset=UTF-8"
break
case "xlsx":
ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
break
default:
ContentType = "image/jpeg"
break
}
console.log(ContentType)
const blog = new Blob([data], {
type: ContentType
})
const url = window.URL.createObjectURL(blog)
const a = document.createElement("a")
a.download = `${name}` // 拼接得到图片名称
a.href = url
a.click()
a.remove()
}
调用的话直接
downLoadFn(data,name)
// data为将要下载的文件数据 是Blob格式的
// name为文件名称要有文件类型后缀例如 “测试.word”