Element Upload上传校验失败后强制删除上传列表中校验失败的那一项

注意:一定要使用ref,方便后面的方法调用

<el-upload 
    ref="uploadCpRef" 
    action="#" 
    list-type="picture-card" 
    :auto-upload="false" 
    :on-change="cpImgChange"
    :file-list="tableData.cpMainImg">
</el-upload>

关键代码:this.$refs.XXX.uploadFiles.splice(this.$refs.XXX.uploadFiles.indexOf(), 1);

cpImgChange(file, fileList){
  const isIMG = ['jpeg', 'png', 'gif', 'jpg'].includes(file.raw.type.split('/')[1]);
  if (!isIMG) {
    this.$refs.uploadCpRef.uploadFiles.splice(this.$refs.uploadCpRef.uploadFiles.indexOf(), 1);
    this.$message.error('上传图片只能是jpeg/png/gif/jpg格式!');
  }
},

注:本人前端小白 ,如有不对的地方还请多多指教

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
element-ui的Upload组件可以用于上传视频。你可以按照以下步骤来实现: 1. 首先,你需要在你的项目引入element-ui组件库。具体可以参考[element-ui官方文档](https://element.eleme.cn/#/zh-CN/component/installation)。 2. 在你的代码使用`<el-upload>`标签来创建上传组件。例如: ``` <template> <div> <el-upload class="upload-demo" action="your-upload-api" :on-success="handleSuccess" :before-upload="beforeUpload" :on-error="handleError" :limit="1" :size="50" :auto-upload="false" :file-list="fileList"> <el-button size="small" type="primary">点击上传视频</el-button> </el-upload> </div> </template> ``` 在这里,`action`属性指定了上传的API地址,`limit`属性限制了上传文件数量,`size`属性限制了上传文件大小,`auto-upload`属性设置为false,表示手动触发上传。 3. 在你的Vue组件定义`beforeUpload`和`handleSuccess`方法,分别用于上传前的校验上传成功后的处理。例如: ``` <script> export default { data() { return { fileList: [] }; }, methods: { beforeUpload(file) { const isVideo = file.type.indexOf('video/') === 0; if (!isVideo) { this.$message.error('只能上传视频文件'); } const isLt50M = file.size / 1024 / 1024 < 50; if (!isLt50M) { this.$message.error('上传视频文件大小不能超过 50MB'); } return isVideo && isLt50M; }, handleSuccess(response, file, fileList) { this.fileList = fileList; this.$message.success('上传成功'); }, handleError(error, file, fileList) { this.$message.error('上传失败'); } } }; </script> ``` 在这里,`beforeUpload`方法用于校验上传文件是否符合要求,`handleSuccess`方法用于处理上传成功后的逻辑,`handleError`方法用于处理上传失败后的逻辑。 4. 最后,你需要在你的后端API实现视频文件上传逻辑。 以上就是使用element-ui的Upload组件上传视频的简单示例。希望能帮到你。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值