element ui上传组件upload设置格式、大小限制后,文件依然显示在列表内的解决方法

element ui上传组件upload设置格式、大小限制后,文件依然显示在列表内的解决方法

按照element ui文档给upload设置上传限制后(代码如下),点击上传弹出错误提示后图片依然会加入到列表中,并自动调用:before-remove的 beforeRemove方法,弹出移除提示框(如下图)

//图片上传限制条件
beforeUpload(file) {
      this.file = file;
        const isLt2M = file.size / 1024 / 1024 < 1;
        if (!isLt2M) {
          this.$message.error("上传图片大小不能超过1MB!");
        }
      return isLt2M;
    },
//移除前提示框
beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}`);
    },

在这里插入图片描述 去网上找了方法,说是要将return isLt2M;改为return false;执行后问题依然没能解决。
经思考后,认为设置限制条件的beforeUpload方法中return false应是返回file 的一种状态,以区分未加限制条件的file状态;
并在移除提示框beforeRemove判断this.file的状态
,如下:

//图片上传限制条件
beforeUploadPic(file) {
      this.file = file;
      const isLt2M = file.size / 1024 / 1024 < 1;
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过1MB!");
        this.file=false;
        return this.file;
      }
    },
//移除前提示框
beforeRemove(file, fileList) {
      if (this.file) {
        return this.$confirm(`确定移除 ${file.name}`);
      }
    },

成功解决上述问题。
在这里插入图片描述

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
el-uploadElement UI中的一个文件上传组件,它可以帮助我们轻松地实现文件上传功能。下面是一个简单的示例,演示如何使用el-upload实现文件上传: 1. 首先需要安装Element UI库并引入相关文件,可以在Vue的入口文件中添加以下代码: ``` import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI) ``` 2. 接着在Vue组件中添加el-upload组件,例如: ``` <template> <el-upload class="upload-demo" action="/api/upload" :on-success="handleSuccess" :before-upload="beforeUpload" :on-error="handleError" :limit="1" :auto-upload="false" :file-list="fileList"> <el-button size="small" type="primary">点击上传</el-button> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> </el-upload> </template> <script> export default { data() { return { fileList: [] } }, methods: { beforeUpload(file) { const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' const isLt2M = file.size / 1024 / 1024 < 2 if (!isJPG) { this.$message.error('上传头像图片只能是 JPG/PNG 格式!') } if (!isLt2M) { this.$message.error('上传头像图片大小不能超过 2MB!') } return isJPG && isLt2M }, handleSuccess(response, file, fileList) { this.fileList = fileList this.$message.success('上传成功') }, handleError(error, file, fileList) { this.fileList = fileList this.$message.error('上传失败') } } } </script> ``` 在上面的示例中,el-upload组件有多个属性和事件,其中比较重要的包括: - action: 上传文件的URL地址。 - on-success: 文件上传成功后的回调函数。 - before-upload: 文件上传前的钩子函数,用于校验文件类型和大小等。 - on-error: 文件上传失败后的回调函数。 - limit: 限制上传文件的数量。 - auto-upload: 是否在选取文件后立即上传。 - file-list: 已上传文件列表。 通过以上步骤,就可以使用el-upload实现文件上传功能了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_45024732

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值