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

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

相关推荐
小周同学:11 小时前
Vue项目中将界面转换为PDF并导出的实现方案
javascript·vue.js·pdf
我命由我1234512 小时前
Excel - Excel 列出一列中所有不重复数据
经验分享·学习·职场和发展·word·powerpoint·excel·职场发展
archko15 小时前
android pdf框架-15,mupdf工具与其它
android·pdf
东方芷兰16 小时前
LLM 笔记 —— 08 Embeddings(One-hot、Word、Word2Vec、Glove、FastText)
人工智能·笔记·神经网络·语言模型·自然语言处理·word·word2vec
知识分享小能手16 小时前
微信小程序入门学习教程,从入门到精通,自定义组件与第三方 UI 组件库(以 Vant Weapp 为例) (16)
前端·学习·ui·微信小程序·小程序·vue·编程
东风西巷19 小时前
MobiPDF安卓版(PDF阅读编辑工具) 修改版
学习·pdf·电脑·软件需求
qq_2052790519 小时前
Unity 项目外部浏览并读取PDF文件在RawImage中显示,使用PDFRender插件
pdf
E_ICEBLUE21 小时前
Python 处理 Word 文档中的批注(添加、删除)
开发语言·python·microsoft·word
郑..方..醒1 天前
java实现ofd转pdf
java·pdf