解决element-ui的upload组件上传文件失败后,仍显示在列表上(成功才显示,失败不显示)

el-upload 上传失败后,文件仍显示在列表上,如图:

在这里插入图片描述
这个pdf文件上传失败,仍显示在列表,给人错觉是上传成功的,所以,我们把它去掉~,下面上代码:

<el-upload
	class="upload-file padding"
	ref="uploadFile"
	multiple
	accept="image/jpeg,image/png,image/gif,application/pdf"
	:http-request="uploadFile"
	:on-change="onChange"
	:before-upload="beforeuploadFile"
	action="123"
	:disabled="!isDisabled"
	:on-remove="handleRemove"
	:file-list="ruleForm.commission">
	<el-button type="primary" size="mini" :loading="uploadLoding" :disabled="!isDisabled">上传</el-button>
	<div slot="tip" class="el-upload__tip" style="line-height: 1; margin: 0 0 10px;">注:只能上传图片或pdf文件</div>
</el-upload>
handleRemove (file, fileList) {
    this.ruleForm.commission = fileList
    // this.ruleForm.fileName = '' // 用于校验
},
onChange (file, fileList) {
  this.files = fileList
},
uploadFile (fileObj) { // 上传
	let file = fileObj.file
	uploadImageInfo({ file: file, width: 165, height: 245, caseId: this.caseId, taskId: this.taskId }).then(res => {
          if (res.success && res.data) {
          	// 后端接口返回后逻辑-todo
              const obj = {
                  name: '文件名.pdf', // 文件名带后缀
                  url: data.path, // 文件路径
              }
              this.ruleForm.commission.push(obj) // 显示用的
          }
      }).catch(err => {
          console.log(err)
          let uid = file.uid // 关键作用代码,去除文件列表失败文件
          let idx = this.$refs.uploadFile.uploadFiles.findIndex(item => item.uid === uid) // 关键作用代码,去除文件列表失败文件(uploadFiles为el-upload中的ref值)

          this.$refs.uploadFile.uploadFiles.splice(idx, 1) // 关键作用代码,去除文件列表失败文件

      })
}

按照以上代码操作,完美地去掉了失败文件~

总结:翻了官方文档,没有看到处理方法,之所以这么处理,是因为显示的fileList列表并不是我赋值数据需要显示的列表。
正好遇到此问题,整理出来解决方法,希望能帮到需要的人,谢谢~
  • 23
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值