Vue3 + Element-Plus upload组件封装限制上传数量,再次上传则覆盖

一、需求描述

在 Vue3 项目中使用 Element Plus 封装上传组件。限制上传文件的大小、实现拖拽上传、限制文件只能上传一个,再次上传则覆盖原来的文件

二、实现效果

在这里插入图片描述

三、源码

我的技术栈:Vue3 + Ts + Vite + Element Plus

<template>
  <el-upload
    class="upload-demo"
    drag
    ref="upload"
    action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
    :limit="1"
    :on-exceed="handleExceed"
    :auto-upload="false"
    :on-change="handleChangeFile"
  >
    <el-icon class="el-icon--upload"><upload-filled /></el-icon>
    <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
    <template #tip>
      <div class="el-upload__tip"> 只能上传一个文件,文件大小不超过3M </div>
    </template>
  </el-upload>
  <el-button class="ml-3" type="success" @click="submitUpload"> upload to server </el-button>
</template>

<script setup lang="ts">
import { UploadFilled } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox, genFileId } from 'element-plus'
import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus'
import { ref } from 'vue'

const upload = ref<UploadInstance>()

/**
 * 校验文件上传大小,若还需校验文件格式,请自行增加代码
 */
const handleChangeFile = (file: any, fileList: any) => {
  if (!file) return
  //限制上传文件大小
  const fileSize = file.size / 1024 / 1024
  if (fileSize > 3) {
    const currIdx = fileList.indexOf(file)
    fileList.splice(currIdx, 1)
    ElMessage.warning('上传文件大小不能超过 3MB!')
  }
}

/**
 *  限制只能上传一个文件,再次上传则覆盖之前的文件
 */
const handleExceed: UploadProps['onExceed'] = (files) => {
  ElMessageBox.confirm('重复上传会覆盖之前的材料,是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      {
        const file = files[0] as UploadRawFile // 拿到第二次上传的文件
        const fileSize = file.size / 1024 / 1024 // 获取改文件的文件大小
        if (fileSize < 3) {
          //  文件大小复符合规则,将上一个文件清空,将新文件上传进行覆盖
          // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
          upload.value!.clearFiles()
          file.uid = genFileId()
          // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
          upload.value!.handleStart(file)
          ElMessage({
            type: 'success',
            message: '覆盖成功'
          })
        } else {
          ElMessage.warning('上传文件大小不能超过 3MB!')
        }
      }
    })
    .catch(() => {
      // ElMessage({
      //   type: 'info',
      //   message: 'Delete canceled'
      // })
    })
}

/**
 * 手动上传方法(因为我需要手动上传,所以将 auto-upload 设置为了false )
 */
const submitUpload = () => {
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  upload.value!.submit()
}
</script>

<style lang="scss" scoped>
.upload-demo {
  width: 100%;
}
</style>

</style>

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue 3中,可以使用 `v-for` 指令和 `provide/inject` API 来实现父组件校验多个子组件的表单。 首先,在子组件中定义一个表单对象,并且使用 `provide` API将表单对象提供给父组件,例如: ``` <template> <el-form :model="form"> ... </el-form> </template> <script> import { provide } from 'vue'; export default { data() { return { form: { ... } } }, mounted() { provide('form', this.form); } } </script> ``` 然后,在父组件中使用 `v-for` 指令和 `inject` API 获取所有子组件的表单对象,例如: ``` <template> <el-form ref="parentForm"> <child-form v-for="(item, index) in items" :key="index"></child-form> ... </el-form> </template> <script> import { inject } from 'vue'; export default { data() { return { items: [...] } }, methods: { validateChildForms() { const childForms = this.items.map((item, index) => { return inject('form', null, true /* throw error if not found */); }); let isValid = true; for (const childForm of childForms) { if (!childForm.validate()) { isValid = false; } } return isValid; } } } </script> ``` 上述代码中,子组件在 `mounted` 钩子函数中使用 `provide` API将表单对象提供给父组件,父组件使用 `v-for` 指令渲染多个子组件,并且使用 `inject` API获取子组件的表单对象。然后,在父组件中的 `validateChildForms` 方法中,遍历所有子组件的表单对象进行校验。 需要注意的是,这里使用 `inject` API 的第三个参数为 `true`,表示如果没有找到提供的表单对象,则会抛出错误。如果子组件没有提供表单对象,或者表单对象提供的名称不是 `form`,则会抛出错误。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

礼貌而已

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

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

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

打赏作者

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

抵扣说明:

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

余额充值