el-upload的多个文件与单个文件上传

样式图:
场景多个:

使用el-upload上传多个文件

javascript 复制代码
<el-upload class="upload-demo" :action="uploadUrl" :on-remove="handleRemove1"
    :on-success="handleAvatarSuccess1"  multiple :limit="5" 
    :on-exceed="handleExceed1" :file-list="fileList1">
     <el-button size="small" type="primary">点击上传</el-button>
</el-upload>

multiple 就是可以上传多个的参数

文件的上传地址 :https://xxx.com/xxx/upload  配置在env文件
uploadUrl: process.env.VUE_APP_XXX配置的名字

handleAvatarSuccess1:上传成功后的函数
handleAvatarSuccess1(res, file, fileList) {  不同的接口的返回参数可能不一样
            let obj = {};
            obj.fileName = res.data.fileName;
            obj.fileUrl = res.data.filePath;
            this.bothFile1.push(obj);
        },


handleRemove1:移除已经上传的文件
bothFile1 :是要发送给接口的参数
handleRemove1(file, fileList) {      多个和单个的这里在我这篇文章中不一样
           this.bothFile1 = this.bothFile1.filter((item) => {
              return item.fileUrl != file.response.data.filePath;
         });
},

handleExceed1 :超过限制以后
 handleExceed1(files, fileList) {
            this.$message.warning(
                `当前限制选择 5 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`
            );
        },

fileList1:是上传后显示在按钮下面的文件列表
由于这里是在el-dialog里面,因此如果关闭后或者上传成功后fileList1要清空
场景单个:
javascript 复制代码
   <el-upload :action="uploadBothPdf" :file-list="fileList3" :on-exceed="handleExceed3"
        :on-success="handleAvatarSuccess3" multiple :limit="1" >
         el-button size="small" type="primary">点击上传</el-button>
   /el-upload>

差别在于,由于只能上传一个文件所以就不需要remove,因为文件是必传,如果是非必选则需要写on-remove 在bothFles(传递的给接口的参数)里面去掉被删除的文件
  handleAvatarSuccess3(res, file) {
            this.bothFile3.fileName = res.data.fileName;
            this.bothFile3.fileUrl = res.data.filePath;
    },
相关推荐
aiprtem21 分钟前
基于Flutter的web登录设计
前端·flutter
浪裡遊24 分钟前
React Hooks全面解析:从基础到高级的实用指南
开发语言·前端·javascript·react.js·node.js·ecmascript·php
why技术31 分钟前
Stack Overflow,轰然倒下!
前端·人工智能·后端
幽络源小助理35 分钟前
SpringBoot基于Mysql的商业辅助决策系统设计与实现
java·vue.js·spring boot·后端·mysql·spring
GISer_Jing36 分钟前
0704-0706上海,又聚上了
前端·新浪微博
止观止1 小时前
深入探索 pnpm:高效磁盘利用与灵活的包管理解决方案
前端·pnpm·前端工程化·包管理器
whale fall1 小时前
npm install安装的node_modules是什么
前端·npm·node.js
烛阴1 小时前
简单入门Python装饰器
前端·python
袁煦丞2 小时前
数据库设计神器DrawDB:cpolar内网穿透实验室第595个成功挑战
前端·程序员·远程工作
天天扭码2 小时前
从图片到语音:我是如何用两大模型API打造沉浸式英语学习工具的
前端·人工智能·github