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

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

相关推荐
sg_knight16 小时前
拥抱未来:ECMAScript Modules (ESM) 深度解析
开发语言·前端·javascript·vue·ecmascript·web·esm
汝生淮南吾在北20 小时前
SpringBoot3+Vue3小区物业报修系统+微信小程序
微信小程序·小程序·vue·毕业设计·springboot·课程设计·毕设
苏打水com1 天前
第十九篇:Day55-57 前端工程化进阶——从“手动低效”到“工程化高效”(对标职场“规模化”需求)
前端·css·vue·html
思杰软件1 天前
pdf发票免费拼图打印
pdf
十月不到底1 天前
vue3手机端列表加载组件
前端·vue
@AfeiyuO1 天前
Vue3 热力图
vue·echarts
IT教程资源D1 天前
[N_115]基于springboot,vue教务管理系统
mysql·vue·前后端分离·springboot教务系统
嘿siri1 天前
uniapp enter回车键不触发消息发送,已解决
前端·前端框架·uni-app·vue
@AfeiyuO1 天前
Vue3 词云
vue·echarts
E_ICEBLUE1 天前
PDF 文件为什么打不开?常见原因与解决思路
pdf·c#·html