element upload 文件缩略图模板 同时上传视频和图片并回显 组件封装

记录一次使用上传文件的缩略图模板并且   区分上传文件格式,手写放大显示跟删除方法  以及回显   并封装成组件,比较粗减。

照片墙

使用 list-type 属性来设置文件列表的样式。

但是用的时候上传视频时   回显图片找不到  于是改写一下 封装成组件 

在components里新建vue页面

<template>
  <div>
    <el-upload
      :limit="limit"
      :action="uploadImgUrl"
      :file-list="UrlList"
      list-type="picture-card"
      :on-change="handleImgChange"
      :on-success="handleUploadSuccess"
      :on-exceed="handleExceed"
    >
      <i slot="default" class="el-icon-plus"></i>
      <div slot="file" slot-scope="{ file }">
        <img
          v-if="file.type == 1"
          class="el-upload-list__item-thumbnail"
          :src="file.url"
          alt=""
        />
        <video v-if="file.type == 2" :src="file.url" style="width: 100%">
          您的浏览器不支持 video 标签。
        </video>
        <audio
          controls
          v-if="file.type == 3"
          :src="file.url"
          style="width: 100%"
        >
          您的浏览器不支持该音频格式。
        </audio>
        <span class="el-upload-list__item-actions">
          <span
            class="el-upload-list__item-preview"
            @click="handlePictureCardPreview(file)"
          >
            <i class="el-icon-zoom-in"></i>
          </span>
          <span
            v-if="!disabled"
            class="el-upload-list__item-delete"
            @click="handleImgRemove(file)"
          >
            <i class="el-icon-delete"></i>
          </span>
        </span>
      </div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible" width="50%">
      <img
        v-if="dialogType == 1"
        class="el-upload-list__item-thumbnail"
        width="100%"
        :src="dialogImageUrl"
        alt=""
      />
      <video
        v-if="dialogType == 2"
        style="width: 100%"
        :src="dialogImageUrl"
        controls
        autoplay
      >
        您的浏览器不支持 video 标签。
      </video>
      <audio controls v-if="dialogType == 3">
        <source :src="dialogImageUrl" />
        您的浏览器不支持该音频格式。
      </audio>
    </el-dialog>
  </div>
</template>

 方法用emit监听

<script>
import bus from "../../utils/bus"; 
export default {
  props: {
    // imageUrlList: {
    //   type: Array,
    //   default: () => [],
    // },
    doBySelf: {
      type: Boolean,
      default() {
        return false;
      },
    },
  },
  data() {
    return {
      limit: 3, //限制上传数量
      uploadImgUrl: process.env.VUE_APP_WEB_URL + "", // 上传的图片服务器地址
      dialogImageUrl: null,
      dialogVisible: false,
      UrlList: [],
      disabled: false,
      dialogType: null,
    };
  },
  methods: {
    //上传
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogType = file.type;
      this.dialogVisible = true;
    },
    // 图片改变
    handleImgChange(file, fileList) { 
        bus.$emit("changeFileAfterList", fileList);
        this.UrlList = fileList; 
    },
    // 图片移除
    handleImgRemove(file, fileList) {
      fileList = this.UrlList; 
        let index = fileList.indexOf(file);
        fileList.splice(index, 1);
        bus.$emit("changeFileAfterList", fileList); 
    },
    //图片上传成功
    handleUploadSuccess(response, file, fileList) {
      file.type = this.matchType(file.response.data.url);
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件,本次选择了 ${files.length} 个文件,共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },

    // 根据文件名后缀区分 文件类型
    /*
     * @param: fileName - 文件名称
     * @param: 数据返回 1) 无后缀匹配 - false
     * @param: 数据返回 2) 匹配图片 - image
     * @param: 数据返回 8) 匹配 视频 - video
     * @param: 数据返回 9) 匹配 音频 - radio
     * @param: 数据返回 10) 其他匹配项 - other
     */
    matchType(fileName) {
      // 后缀获取
      var suffix = "";
      // 获取类型结果
      var result = "";
      try {
        var flieArr = fileName.split(".");
        suffix = flieArr[flieArr.length - 1];
      } catch (err) {
        suffix = "";
      }
      // fileName无后缀返回 false
      if (!suffix) {
        result = false;
        return result;
      }
      // 图片格式
      var imglist = ["png", "jpg", "jpeg", "bmp", "gif"];
      // 进行图片匹配
      result = imglist.some(function (item) {
        return item == suffix;
      });
      if (result) {
        result = "1";
        return result;
      }
      // 匹配 视频
      var videolist = ["mp4", "m2v", "mkv"];
      result = videolist.some(function (item) {
        return item == suffix;
      });
      if (result) {
        result = "2";
        return result;
      }
      // 匹配 音频
      var radiolist = ["mp3", "wav", "wmv", "flac"];
      result = radiolist.some(function (item) {
        return item == suffix;
      });
      if (result) {
        result = "3";
        return result;
      }
      // 其他 文件类型
      result = "other";
      return result;
    },
  },
};
</script>

在父页面中调用 

   <FileUploadEdit ref="UrlList"></FileUploadEdit>

import FileUploadEdit from "@/components/FileUploadEdit";

 components: {
    FileUploadEdit, 
  },

父页面监听数据变化 写在created()方法里,

   bus.$on("changeFileList", (fileList) => {
      this.ruleForm.imageUrlList = [];
      this.ruleForm.videoUrlList = [];
      this.ruleForm.musicUrlList = [];
      if (fileList.length > 0) {
        fileList.map((item) => {
          if (item.response) {
            if (fileUploadIf(item.response.data.url) == 1) {
              this.ruleForm.imageUrlList.push(item.response.data.url);
            } else if (fileUploadIf(item.response.data.url) == 2) {
              this.ruleForm.videoUrlList.push(item.response.data.url);
            } else if (fileUploadIf(item.response.data.url) == 3) {
              this.ruleForm.musicUrlList.push(item.response.data.url);
            }
          }  
        });
      }
    });

以上是上传文件改写的封装。

/utils/bus.js  自己新建一下

// bus.js
import Vue from 'vue';

// 使用 Event Bus
const bus = new Vue;

export default bus;

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
要使用element-plus、ts和vue3实现上传多张图片回显,可以按照以下步骤操作: 1. 首先,安装element-plus和vue3的相关依赖。可以使用npm或yarn安装element-plus和vue3的依赖包。 2. 在项目中创建一个上传组件,可以使用element-plus提供的el-upload组件实现。在组件中,引入和注册el-upload组件。 3. 组件中,声明一个data属性,用于存储图片文件回显图片列表。可以使用ref或reactive创建响应式数据。 4. 在template模板中,使用el-upload组件,设置一些必要的属性,如action、listType和multiple等。通过action属性指定上传的地址,listType属性设置为"picture-card"以展示上传图片,并且通过multiple属性支持上传多张图片。 5. 设置el-upload组件的onSuccess和beforeRemove事件,用于在成功上传图片或删除图片时触发相应的逻辑。在onSuccess事件中,将上传文件添加到data属性中的图片文件列表中;在beforeRemove事件中,通过索引将要删除的图片从data属性中的图片文件列表中移除。 6. 在组件中,使用v-for指令遍历图片文件列表,并使用el-image组件展示回显图片。el-image组件element-plus提供的用于展示图片组件。 7. 最后,在应用的父组件中,使用刚刚创建的上传组件。使用v-model指令绑定父组件中的数据,以获取上传成功后的图片文件。 通过以上步骤,就可以实现使用element-plus、ts和vue3上传多张图片回显的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值