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>
相关推荐
小小码农Come on6 小时前
QML访问子项内容
前端·javascript·html
幽络源小助理7 小时前
小六壬排盘工具源码 自适应双端 纯原生HTML+JS
前端·javascript·html
Championship.23.248 小时前
Open Source Pipeline Skill深度解析:自动化开源贡献全流程
前端·javascript·html
凯瑟琳.奥古斯特11 小时前
Bootstrap快速上手指南
开发语言·前端·css·bootstrap·html
恶猫11 小时前
网页自动化模拟操作时,模拟真实按键触发事件【终级方案】
前端·javascript·自动化·vue·网页模拟
Dxy123931021611 小时前
HTML中的Canvas可以干哪些事情
前端·html
悟乙己11 小时前
解析 Agent 时代的 HTML PPT SKILLS: html-ppt-skill
前端·html·powerpoint
ZC跨境爬虫11 小时前
跟着 MDN 学 HTML day_2:(表单分组与高级输入控件实战)
前端·javascript·css·ui·html
Dxy123931021613 小时前
HTML中使用Canvas动态图形渲染:解锁Web交互新维度
前端·html·图形渲染
ZC跨境爬虫14 小时前
跟着 MDN 学 HTML day_1:(全套原生Input+表单结构拆解)
前端·css·ui·html