Vue 使用 ElementUi el-upload 手动上传文件限制同名文件

<el-upload
  class="upload-demo"
  action="#"
  multiple
   :auto-upload="false"
  list-type="picture-card"
  :file-list="modelForm.fileList"
  :show-file-list="false"
  :limit="3"
  :on-change="handleChange"
  >
  <el-button size="small" type="primary">点击上传文件</el-button>
</el-upload>

// 文件状态改变时
handleChange(file) {
	let { name , uid } = file
	// 利用findIndex方法查询名字是否重复,不重复返回-1
	const findCommonNameIndex = this.modelForm.fileList.findIndex(
	        item => item.name == name
	      )
    // 如果重复则findCommonNameIndex为-1,进去判断进行相应操作
    if (findCommonNameIndex !== -1) {
      this.$message.warning('不能上传同名的文件')
      const selectFileList = this.modelForm.fileList.filter(item => {
        return item.uid != uid
      })
      this.modelForm.fileList = selectFileList
      return
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
el-uploadElement UI 中的一个上传组件,可以用来实现文件上传功能。以下是使用 el-upload 手动上传文件的步骤: 1. 首先,需要在页面中引入 el-upload 组件,如下所示: ```html <el-upload class="upload-demo" action="/upload" :on-success="handleSuccess" :before-upload="beforeUpload" :auto-upload="false"> <el-button slot="trigger" type="primary">选取文件</el-button> <el-button type="success" @click="submitUpload">上传到服务器</el-button> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> </el-upload> ``` 其中,action 属性指定了上传文件的接口地址,on-success 属性指定了上传成功后的回调函数,before-upload 属性指定了上传前的操作函数,auto-upload 属性设置为 false,表示需要手动触发上传操作。 2. 在 Vue 组件中定义相关的函数: ```javascript methods: { handleSuccess(response, file, fileList) { console.log(response, file, fileList); }, 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; }, submitUpload() { this.$refs.upload.submit(); } } ``` 其中,handleSuccess 函数是上传成功后的回调函数,beforeUpload 函数是上传前的操作函数,submitUpload 函数用于手动触发上传操作。 3. 最后,需要在后端实现上传接口,接收并处理上传文件。具体实现方式可以根据具体情况进行选择,这里不再赘述。 以上就是使用 el-upload 手动上传文件的简单步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值