Vue3 上传后的文件智能预览(实战体会)

目录

  • 前言
  • [1. Demo1](#1. Demo1)
  • [2. Demo2](#2. Demo2)

前言

🤟 找工作,来万码优才:👉 #小程序://万码优才/r6rqmzDaXpYkJZF
爬虫神器,无代码爬取,就来:bright.cn

此处的基本知识涉及较少,主要以Demo的形式供大家学习,从实战中触发

本身url是在线链接且是以数组的形式存在

开源项目来源:https://gitee.com/zhijiantianya/ruoyi-vue-pro

1. Demo1

本身一开始以Minio的形式上传,以文件的形式进行命名:

后续用户需要一个个点击才能看到是什么文件

html 复制代码
 <el-form-item label="单证附件" prop="imgPath">
   <UploadFile v-model="formData.imgPath" limit="10" />
 </el-form-item>
  1. 把 imgPath 按逗号分割为数组

  2. 遍历数组,每个链接:

    如果是图片(比如后缀是 .jpg、.png 等),就渲染成 <el-image>

    如果不是图片,就渲染成带下载链接的文件名

示例的Demo如下:

html 复制代码
<template>
  <div class="file-preview-list">
    <div
      v-for="(item, index) in fileList"
      :key="index"
      class="preview-item"
    >
      <el-image
        v-if="isImage(item)"
        :src="item"
        :preview-src-list="[item]"
        fit="cover"
        style="width: 100px; height: 100px; margin-right: 10px;"
      >
        <template #error>
          <div style="font-size: 12px; color: #999;">加载失败</div>
        </template>
      </el-image>

      <a
        v-else
        :href="item"
        target="_blank"
        style="color: #409EFF; text-decoration: underline;"
      >
        文件 {{ index + 1 }}
      </a>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue';

const props = defineProps({
  modelValue: String
});

const fileList = computed(() => {
  return props.modelValue
    ? props.modelValue.split(',').map(item => item.trim())
    : [];
});

const isImage = (url) => {
  return /\.(jpg|jpeg|png|gif|bmp|webp)$/i.test(url);
};
</script>

<style scoped>
.file-preview-list {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}
.preview-item {
  display: flex;
  align-items: center;
}
</style>

组件这样使用:

html 复制代码
<el-form-item label="单证附件" prop="imgPath">
  <UploadFile v-model="formData.imgPath" limit="10" />
  <UploadPreview v-model="formData.imgPath" />
</el-form-item>

后续由于图片有些过大,对应以正在加载的形式呈现:

html 复制代码
<template>
  <div class="file-preview-list">
    <div
      v-for="(item, index) in fileList"
      :key="index"
      class="preview-item"
    >
      <el-image
        v-if="isImage(item)"
        :src="item"
        :preview-src-list="[item]"
        fit="cover"
        style="width: 100px; height: 100px; margin-right: 10px;"
      >
        <template #placeholder>
          <div
            style="display: flex; align-items: center; justify-content: center; height: 100%; color: #aaa; font-size: 12px;"
          >
            正在加载...
          </div>
        </template>
        <template #error>
          <div style="font-size: 12px; color: #999;">加载失败</div>
        </template>
      </el-image>

      <a
        v-else
        :href="item"
        target="_blank"
        style="color: #409EFF; text-decoration: underline;"
      >
        文件 {{ index + 1 }}
      </a>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue';

const props = defineProps({
  modelValue: String
});

const fileList = computed(() => {
  return props.modelValue
    ? props.modelValue.split(',').map(item => item.trim())
    : [];
});

const isImage = (url) => {
  return /\.(jpg|jpeg|png|gif|bmp|webp)$/i.test(url);
};
</script>

<style scoped>
.file-preview-list {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}
.preview-item {
  display: flex;
  align-items: center;
}
</style>

✅ 推荐结构(用 el-row + 两个 el-col)

原来的结构是把所有内容都放在了一个 el-form-item 里面,这样不太好控制布局

建议改成下面这样:

html 复制代码
<el-row>
  <!-- 左侧:上传控件 -->
  <el-col :span="12">
    <el-form-item label="单证附件" prop="imgPath">
      <UploadFile v-model="formData.imgPath" limit="10" />
    </el-form-item>
  </el-col>

  <el-col :span="12">
    <div style="margin-bottom: 8px; font-weight: bold;">附件预览</div>
    <UploadPreview v-model="formData.imgPath" />
  </el-col>
</el-row>

最终截图如下:

2. Demo2

另外一种呈现的方式如下:

html 复制代码
<el-table-column label="照片" align="center" prop="imgPath" width="500" fixed="left">
	<template #default="{ row }">
	  <div v-if="row.imgPath && row.imgPath.length > 0" class="damage-images">
	    <el-image
	      v-for="(img, index) in row.imgPath"
	      :key="index"
	      class="h-80px w-80px"
	      lazy
	      :src="img"
	      :preview-src-list="row.imgPath"
	      preview-teleported
	      fit="cover"
	    />
	  </div>
	  <div v-else class="no-image">无图片</div>
	</template>
</el-table-column>
相关推荐
Metaphor6926 小时前
使用 Python 将 PDF 转换为 HTML
python·pdf·html
a1117768 小时前
“黑夜流星“个人引导页 网页html
java·前端·html
JieE2129 小时前
手把手带你用纯 CSS 实现一个 3D 旋转魔方,这些前端基础你能打几分?
前端·css·html
YHL11 小时前
🧊 CSS 3D 硬核解析:四个属性手写旋转立方体
前端·css·html
a11177611 小时前
无限森林漫游(简约几何版 html
前端·html
LaughingZhu13 小时前
Product Hunt 每日热榜 | 2026-06-16
前端·人工智能·经验分享·chatgpt·html
m0_547486661 天前
《HTML+CSS+JavaScript+Vue前端开发技术教程》全套PPT课件
javascript·css·html
fastjson_1 天前
Edge浏览器开启IE兼容模式
javascript·edge·html
艾伦野鸽ggg1 天前
web 组大一下第二次考核
前端·css·html
货拉拉技术1 天前
Huolala Figma MCP 原理与实践
人工智能·前端框架·html