自定义Element ui中el-upload上传后的文件图标

本文介绍如何使用ElementUI的el-upload组件实现根据不同类型的文件上传显示不同的图标,包括图片、文档和视频等,并提供了具体的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现功能:上传图标显示图片图标,上传文件显示文档图标,上传视频显示视频图标
首先想到的是去Element UI 查看有没有自定义的功能,后面发现底层源码写死的,那就不能自定义,就只能通过js操作了,我就在上传成功的钩子函数处理了
el-upload组件

<el-upload
        style="display: flex; align-items: center"
 		ref="upload"
       accept="image/png, image/gif, application/msword, text/plain, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.openxmlformats-officedocument.wordprocessingml.document, aplication/zip, aplication/rar, audio/mp4, video/mp4"
        multiple
        :limit="5"
        :action="loadhttp"
        :on-change="handleChange"
        :on-exceed="handleExceed"
        :file-list="fileList"
        :auto-upload="true"
        :before-upload="beforeUpload"
        :on-success="loadsuccess"
        :before-remove="loadremove"
        :headers="getheaders"
       >
         <i class="el-icon-upload"></i>
         <span>点击上传</span>
   </el-upload>

loadSucess方法

loadsuccess (response, file, fileList) {
      var houzhui = file.name.split('.') // 获取上传文件的后缀
      var title = document.getElementsByClassName('el-icon-document')[fileList.length-1] 
      // [fileList.length-1]  这个需要特别注意,需要查看当前页面不上传之前有多少个后才能根据具体情况具体分析
      if (houzhui[1]=='png'||houzhui[1]=='jpg'||houzhui[1]=='jpeg'||houzhui[1]=='gif') { 
        title.classList.add('el-icon-picture-outline') // 图片,动图
      } else if (houzhui[1]=='MP4'||houzhui[1]=='mp4'||houzhui[1]=='avi') {
        title.classList.add('el-icon-video-camera') // 视频
      } else {
        title.classList.add('el-icon-document') // 其他默认文档
      }
		...................//此处正常的写文件处理流程就行了
		},

在这里插入代码片


Element Plus 的 `el-upload` 组件是一个强大的文件上传组件,支持一次性上传多个文件。在使用它上传文件时,可以按照以下步骤操作: 1. 引入依赖:首先,在 Vue 项目中安装 Element Plus,并在需要使用的组件中导入 `ElUpload` 和相关的样式。 ```html <template> <div> <el-upload :action="uploadUrl" :multiple="true" <!-- 显示为多选模式 --> :on-change="handleChange" <!-- 文件选择改变事件 --> :before-upload="beforeUpload" <!--上传钩子 --> :file-list="fileList" <!-- 存储已上传文件列表 --> ref="uploadRef"> <!-- 指定文件上传按钮或区域 --> <i class="el-icon-plus"></i> 选择文件 </el-upload> </div> </template> <script> import { ElUpload } from 'element-plus'; export default { components: { ElUpload, }, data() { return { uploadUrl: 'your-api-url', // 你的服务器接收文件的URL fileList: [], // 初始化为空数组 }; }, methods: { handleChange(file) { this.fileList.push(file); // 文件选择后,将新文件添加到列表中 }, beforeUpload(file) { // 可在此检查文件大小、类型等限制 if (/* your condition */) { return false; // 返回false阻止上传 } return true; }, }, }; </script> ``` 2. 配置:`before-upload` 函数允许你在上传之前对文件进行一些预处理,如验证文件类型、大小等。如果满足条件,则返回 `true` 启动上传,否则返回 `false` 中止上传。 3. 监听状态:通过 `handleChange` 方法,你可以获取到用户选择的文件列表,以及上传过程中的状态变化。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值