vue el-upload上传组件,如何同时拿到所有的上传文件

场景描述:

点击上传的时候,选中多个文件,同时上传,然后添加一个校验,选中的所有的文件的大小不能超过50M,否则就报错提示。

解决思路:

一.通过给组件绑定一个class

复制代码
<el-upload
      v-if="storageType === 'minio' || storageType === 'ali-oss'"
      :http-request="upload"
      class="upload-file"
      :on-change="handFileChange"
      :accept="accept"
      :list-type="listType"
      action
      :multiple="multiple"
      :before-upload="beforeUpload"
      :on-exceed="handleExceed"
      :limit="limit"
      :on-remove="handleRemove"
      :before-remove="beforeRemove"
      :on-preview="handlePreview"
      :file-list="files"
      :disabled="disabled"
    >
      <div v-if="buttonStyle === 'button'">
        <el-badge :value="badgeValue" :hidden="badgeHidden || !limit" class="item">
          <el-button type="primary">{{ $t('common.button.clickUpload') }}</el-button>
        </el-badge>
        <el-button @click.stop="downloadZip" type="warning">批量下载</el-button>
      </div>
      <div v-if="buttonStyle === 'icon'">
        <el-button class="button-icon" type="text">
          <i class="el-icon-upload" :title="$t('common.button.clickUpload')" />
        </el-button>
      </div>
    </el-upload>

2.然后通过on-change拿到所有的文件数量

复制代码
handFileChange() {
      const upload_img = document.getElementsByClassName('upload-file')
      if (upload_img && upload_img.length > 0) {
        const upload = upload_img[0].getElementsByTagName('input')
        if (upload && upload.length > 0 && upload[0].files && upload[0].files.length > 0) {
          this.uploadNum = upload[0].files.length
          return false
        }
      }
    }

3.添加校验

复制代码
upload(content) {
      this.elements.push(content)
      if (this.elements.length === this.uploadNum) {
        const totalSize = this.elements.reduce((total, element) => total + element.file.size, 0)
        console.log('totalSize', totalSize)
        if (totalSize >= 52428800) {
          return this.$message.error('上传文件大小不得大于50MB!')
        }
        if (this.storageType === 'minio' || this.storageType === 'ali-oss') {
          for (let index = 0; index < this.elements.length; index++) {
            const element = this.elements[index]
            this.minioOssUpload(element)
          }
        }
      }
    }
相关推荐
烟袅2 分钟前
告别 var!深入理解 JavaScript 中 var、let 和 const 的差异与最佳实践
javascript·面试
烛阴3 分钟前
循环背后的魔法:Lua 迭代器深度解析
前端·lua
元拓数智17 分钟前
现代前端状态管理深度剖析:从单一数据源到分布式状态
前端·1024程序员节
mapbar_front21 分钟前
Electron 应用自动更新方案:electron-updater 完整指南
前端·javascript·electron
天一生水water1 小时前
three.js加载三维GLB文件,查看三维模型
前端·1024程序员节
无风听海1 小时前
HarmonyOS之启动应用内的UIAbility组件
前端·华为·harmonyos
冰夏之夜影1 小时前
【科普】Edge出问题后如何恢复出厂设置
前端·edge
W.Buffer2 小时前
设计模式-单例模式:从原理到实战的三种经典实现
开发语言·javascript·单例模式
葱头的故事2 小时前
vant van-uploader上传file文件;回显时使用imageId拼接路径
前端·1024程序员节
Mintopia3 小时前
🇨🇳 Next.js 在国内场景下的使用分析与实践指南
前端·后端·全栈