【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'
    }
  ]"
/>

页面展示


相关推荐
2501_947575801 小时前
计算机毕业设计之jsp开山车行二手车交易系统
java·开发语言·hadoop·python·信息可视化·django·课程设计
骑士雄师2 小时前
java面试题 4:鉴权
java·开发语言
KaMeidebaby2 小时前
卡梅德生物技术快报|蛋白 N 端测序在重组贻贝融合蛋白表征中的应用,解决原核表达序列偏移工艺难题
前端·人工智能·物联网·算法·百度
时间的拾荒人3 小时前
C语言字符函数与字符串函数完全指南
c语言·开发语言
2501_948106913 小时前
计算机毕业设计之基于jsp教科研信息共享系统
java·开发语言·信息可视化·spark·课程设计
kyriewen3 小时前
我筛了 1400 个 Claude Code Skills,留下 5 个天天在用的
前端·ai编程·claude
取经蜗牛3 小时前
Python 第一阶段完全指南:从零到第一个实用工具
开发语言·python
JNX_SEMI4 小时前
AT2401C 2.4GHz 全集成射频前端单芯片技术解析
前端·单片机·嵌入式硬件·物联网·硬件工程
anOnion4 小时前
Agentic 前端开发之 实时显示 AI Agent 终端输出
前端·javascript·人工智能
dog2504 小时前
从重尾到截断流量模型的演进
开发语言·php