vue3下载图片,pdf,excle,word通用函数

接口返回的是数据格式tmplFileUrl:"http://--------/images/cf0a72b3b6ba4c9cb79d3b063bbbed4b.docx"

javascript 复制代码
// 下载图片,pdf,word,exlce文件
const handleDownload = (record: RecordType) => {
  const fileUrl = record.tmplFileUrl
  if (fileUrl) {
    const downloadUrl = getPreviewUrlHandler(fileUrl) || ''
    if (downloadUrl) {
      // 从 tmplFileUrl 中提取文件名和类型
      const fileName = fileUrl.split('/').pop() || record.tmplName || 'download'
      const fileExtension = fileName.split('.').pop()?.toLowerCase() || ''

      // 根据文件扩展名判断文件类型
      const isImageFile = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'].includes(fileExtension)
      const isPdfFile = fileExtension === 'pdf'
      const isWordFile = ['doc', 'docx'].includes(fileExtension)
      const isExcelFile = ['xls', 'xlsx'].includes(fileExtension)
      if (isImageFile) {
        // 图片文件:使用当前下载逻辑,直接下载到本地
        const link = document.createElement('a')
        link.href = downloadUrl
        link.download = fileName
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
      } else {
        // 附件文件:强制下载,不打开预览
        const link = document.createElement('a')
        link.href = downloadUrl
        link.download = fileName
        // 强制下载,防止浏览器打开预览
        link.style.display = 'none'

        if (isPdfFile) {
          // 对于 PDF 文件,添加额外的强制下载处理
          fetch(downloadUrl)
            .then((response) => response.blob())
            .then((blob) => {
              const url = window.URL.createObjectURL(blob)
              link.href = url
              link.download = fileName
              document.body.appendChild(link)
              link.click()
              document.body.removeChild(link)
              // 清理 blob URL
              window.URL.revokeObjectURL(url)
            })
            .catch((error) => {
              console.error('PDF 下载失败:', error)
              // 如果 fetch 失败,回退到普通下载方式
              document.body.appendChild(link)
              link.click()
              document.body.removeChild(link)
            })
        } else if (isWordFile || isExcelFile) {
          // 对于 Word 和 Excel 文件,使用 fetch 方式下载以提升性能
          fetch(downloadUrl)
            .then((response) => {
              if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`)
              }
              return response.blob()
            })
            .then((blob) => {
              const url = window.URL.createObjectURL(blob)
              link.href = url
              link.download = fileName
              document.body.appendChild(link)
              link.click()
              document.body.removeChild(link)
              // 清理 blob URL
              window.URL.revokeObjectURL(url)
            })
            .catch((error) => {
              console.error('文件下载失败:', error)
              // 如果 fetch 失败,回退到普通下载方式
              document.body.appendChild(link)
              link.click()
              document.body.removeChild(link)
            })
        } else {
          // 其他文件使用普通下载方式
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link)
        }
      }
    }
  }
}

简单记录下下载通用函数哈!

相关推荐
帧栈1 天前
SpringBoot + iTextPDF + Acrobat 构建动态PDF表单的完整实践
spring boot·后端·pdf
前端摸鱼匠1 天前
Vue 3 事件修饰符全解析:从 .stop 到 .passive,彻底掌握前端交互的艺术
前端·vue.js·node.js·vue·交互
Crazy Struggle1 天前
.NET 8.0 + Vue 企业级在线培训系统(开源、免费、支持多种主流数据库)
vue·.net 8.0·后台管理系统
红鼻子时代1 天前
从零开始构建PDF文档生成器(二)- 添加页眉页脚
pdf·页眉页脚·canvas绘制
不坑老师2 天前
利用不坑盒子在WPS中插入网页,放映的电脑无需安装插件,直接就能显示网页!
microsoft·word·powerpoint·wps
韩立学长2 天前
【开题答辩实录分享】以《植物病虫害在线答疑小程序的设计与实现》为例进行答辩实录分享
spring boot·小程序·vue
ljh_learn_from_base2 天前
【spring boot 使用apache poi 生成和处理word 文档】
java·spring boot·word·apache
Source.Liu2 天前
【pdf-rs】color.rs 文件解析
pdf
2501_920955572 天前
Word崩溃打不开?实测三款Word文档修复工具!
word
ceffans2 天前
PDF文档中表格以及形状解析-后续处理(线段生成最小多边形)
c++·windows·算法·pdf