【FileShowCom 组件】文件预览:图片预览 el-image,其余文件预览打开新窗口或者下载

这里写目录标题

FileShowCom 组件代码

html 复制代码
<template>
  <div class="file-show-com">
    <template v-for="(file, index) in fileList">
      <el-image
        v-if="isImage(file)"
        :key="'img-' + index"
        class="file-image"
        :src="getFileUrl(file)"
        :preview-src-list="[getFileUrl(file)]"
        fit="cover"
      >
        <div slot="error" class="image-error">
          <i class="el-icon-picture-outline" title="图片加载失败"></i>
        </div>
      </el-image>
      <el-link
        v-else
        :key="'link-' + index"
        type="primary"
        :href="getFileUrl(file)"
        :download="getFileName(file)"
        target="_blank"
      >
        <i class="el-icon-document"></i>
        {{ getFileName(file) }}
      </el-link>
      <span
        v-if="index < fileList.length - 1"
        :key="'separator-' + index"
        class="file-separator"
      ></span>
    </template>
  </div>
</template>

<script>
export default {
  name: 'FileShowCom',
  props: {
    fileList: {
      type: Array,
      default: () => []
    },
    fileNameField: {
      type: String,
      default: 'wjmc'
    },
    filePathField: {
      type: String,
      default: 'wjlj'
    },
    fileTypeField: {
      type: String,
      default: 'wjgs' // 文件格式不带点,例如:jpg,不是.jpg
    }
  },
  computed: {},
  methods: {
    isImage(file) {
      if (!file) return false
      let ext = null
      if (file[this.fileTypeField]) {
        ext = file[this.fileTypeField]
      } else {
        const url = typeof file === 'string' ? file : file[this.filePathField]
        const name = typeof file === 'string' ? file : file[this.fileNameField]
        if (!url && !name) return false
        const urlSplit = url.split('.')
        const nameSplit = name.split('.')
        if (urlSplit.length > 1) ext = urlSplit.pop().toLowerCase()
        else if (nameSplit.length > 1) ext = nameSplit.pop().toLowerCase()
      }
      return ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'svg'].includes(ext)
    },
    getFileUrl(file) {
      return typeof file === 'string' ? file : file[this.filePathField]
    },
    getFileName(file) {
      if (!file) return ''
      if (typeof file === 'string') {
        return file.split('/').pop() || file
      }
      return (
        file[this.fileNameField] ||
        (file[this.filePathField] ? file[this.filePathField].split('/').pop() : '') ||
        ''
      )
    }
  }
}
</script>

<style scoped>
.file-show-com {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
}
.file-image {
  width: 40px;
  height: 40px;
  border-radius: 4px;
  cursor: pointer;
}
.image-error {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  color: #909399;
  font-size: 18px;
}
.file-separator {
  margin: 0 8px;
  color: #dcdfe6;
}
</style>

组件使用

html 复制代码
<FileShowCom
   :file-list="[
    {
      wjmc: 'example.jpg',
      wjlj: 'https://images.dog.ceo/breeds/pembroke/n02113023_5362.jpg'
    },
    {
      wjmc: 'DOC.doc',
      wjlj: 'https://images.dog.ceo/breeds/pembroke/n02113023_5362.doc'
    },
    {
      wjmc: 'MP4.mp4',
      wjlj:
        'https://live-s3m.mediav.com/nativevideo/dec07f3b3b0953722b738ced98fe148d-bit_cloud768.mp4'
    },
    {
      wjmc: 'PDF.pdf',
      wjlj:
        'http://192.168.0.42:9000/oss/ajax/bucket/file/supervisecore/ACTIVITY/20260407/42mZWFifrYySXhPl+4H6Eg==.pdf'
    }
  ]"
/>

页面展示


相关推荐
二级小助手4 分钟前
二级Web前端选择题高频真题20道与考场避坑笔记
javascript·css3·html5·web前端·计算机二级·二级web·web真题
珐恩AI-人工智能15 分钟前
大模型意图召回偏差分析:GEO如何解决“有收录却不触发问答曝光”的难题
大数据·前端·人工智能·html·流量运营·geo优化
城管不管17 分钟前
重生——第十次面试之开源中国一面挂
java·linux·开发语言·算法·面试·职场和发展·开源
霸道流氓气质30 分钟前
Java开发者AI编程常用MCP、Skills、Rules全景汇总
java·开发语言·ai编程
程序员老赵34 分钟前
Docker 部署禅道 ZenTao:轻松搭建研发项目管理平台
前端·后端·github
前端小卡拉41 分钟前
用了大半年 AI 编程工具,我才搞懂 Skill 到底是什么
前端·javascript
hunterandroid43 分钟前
[鸿蒙从零到一] HarmonyOS 单元测试与 UI 自动化测试实战:从代码质量到用户体验的全链路保障
前端
郭邯1 小时前
用 Intl.DateTimeFormat 手写一个时区转换器,顺便聊聊我和 AI 结对编程的日常
前端·javascript
阿奇__1 小时前
微前端、iframe与Auth Code技术方案对比
前端
FungLeo1 小时前
React 管理后台实战 · 前端请求层怎么写?401 静默刷新、token 并发竞争、强制改密拦截一次说清
前端·react.js·前端框架