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)
          }
        }
      }
    }
相关推荐
小墨宝28 分钟前
js 生成pdf 并上传文件
前端·javascript·pdf
HED44 分钟前
用扣子快速手撸人生中第一个AI智能应用!
前端·人工智能
DN金猿1 小时前
使用npm install或cnpm install报错解决
前端·npm·node.js
丘山子1 小时前
一些鲜为人知的 IP 地址怪异写法
前端·后端·tcp/ip
志存高远661 小时前
Kotlin 的 suspend 关键字
前端
www_pp_1 小时前
# 构建词汇表:自然语言处理中的关键步骤
前端·javascript·自然语言处理·easyui
YuShiYue2 小时前
pnpm monoreop 打包时 node_modules 内部包 typescript 不能推导出类型报错
javascript·vue.js·typescript·pnpm
天天扭码2 小时前
总所周知,JavaScript中有很多函数定义方式,如何“因地制宜”?(ˉ﹃ˉ)
前端·javascript·面试
一个专注写代码的程序媛2 小时前
为什么vue的key值,不用index?
前端·javascript·vue.js
vvilkim2 小时前
React 与 Vue:两大前端框架的深度对比
vue.js·react.js·前端框架