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)
        }
      }
    }
  }
}

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

相关推荐
CodeCraft Studio7 小时前
ABViewer 16全新发布:3D可视化、PDF转DWG、G-code生成全面升级
pdf
lqz19938 小时前
根据html导出excel和word
html·word·excel
缺点内向12 小时前
C# 中 Word 文档目录的插入与删除指南
开发语言·c#·word·.net
诸神缄默不语1 天前
如何用Python处理文件:Word导出PDF & 如何用Python从Word中提取数据:以处理简历为例
python·pdf·word
i***66501 天前
SpringBoot实战(三十二)集成 ofdrw,实现 PDF 和 OFD 的转换、SM2 签署OFD
spring boot·后端·pdf
777VG1 天前
Vue3+vue3-pdf-app@1.0.3实现加载 .pdf文件
前端·javascript·vue.js·pdf
ComPDFKit1 天前
Salesforce原生PDF编辑的重要性:效率、合规性与用户体验
大数据·pdf·ux
初九之潜龙勿用1 天前
C# 操作Word模拟解析HTML标记输出带格式的文本
开发语言·c#·word·office
Forever777777771 天前
PDF瘦身,告别WPS收费压缩PDF
python·深度学习·pdf·免费
FreeBuf_1 天前
高危Markdown转PDF漏洞,可通过Markdown前置元数据实现JS注入攻击(CVE-2025-65108,CVSS 10.0)
开发语言·javascript·pdf