重新上传的方法

104 篇文章 0 订阅
这是一个关于前端上传组件的实现细节,包括文件类型检查、大小限制、重新上传和删除功能。组件使用了Element UI的`el-upload`组件,支持文件列表显示,并在上传前进行格式和大小验证,超过限制时会显示错误提示。此外,还提供了重新上传和删除文件的操作方法。
摘要由CSDN通过智能技术生成
  <el-upload
      ref="uploadDemo"
      v-else-if="type == 'file'"
      class="upload-demo logo-uploader"
      :action="action"
      :headers="headers"
      :file-list="fileList"
      :show-file-list="showFileList"
      :on-exceed="exceed"
      :limit="limit"
      :accept="allowType && allowType.toString()"
      :on-success="success"
      :on-progress="progress"
      :multiple="multiple"
      :before-upload="beforeUpload"
    >
      <div class="logo-uploader-icon">
        <div class="upload-box">
          <svg-icon icon-class="icon-upload" size="25">
          </svg-icon>
        </div>
        <div class="">{{ btnName }}</div>
      </div>

    </el-upload>
    <template v-if="type == 'file'">
      <div  class="pdf-item" @click.stop.prevent v-for="(item, index) in fileList" :key="index" style="display: flex;">
          <div class="info">
            <slot name="icon"></slot>
            <span>{{ item.name }}</span>
          </div>
          <span>
            <a href="javascript:;"  @click.stop="reloadUpload">重新上传</a>
            <a href="javascript:;" class="ml-10" @click.stop="delPdf(index)">删除</a>
          </span>
        </div>
    </template>

data(){
return {
  showFileList:false,
}
}

  // 重新上传方法
  reloadUpload() {
      document.querySelector('.el-upload__input').click()
    },
 // 上传之前
 beforeUpload(file) {
      let typeName = this.typeName
      if (this.type == 'file') {
        typeName = '文件'
      }
      if (this.allowType) {
        var index = this.allowType.indexOf(
          file.name.substring(file.name.lastIndexOf('.')).toLowerCase()
        )
        if (index < 0) {
          this.$message({
            message: `${typeName}格式不正确`,
            type: 'warning'
          })
          return false
        }
      }
      if (this.allowSize) {
        // debugger
        const b = file.size
        let isLtSize = b > this.allowSize * 1024
        const sizeStr =
          this.allowSize >= 1024
            ? `${Math.round((this.allowSize / 1024) * 100) / 100}MB`
            : `${this.allowSize}KB`
        const videoType = [
          '.MP4',
          '.mp4',
          '.mmvb',
          '.fliv',
          '.avi',
          '.3gp',
          '.flv'
        ] // '.MP4', '.mp4', '.mmvb', '.fliv', '.avi', '.3gp', '.flv' 视频默认 50M
        let isVideo = false
        if (
          this.defaultSize &&
          videoType.indexOf(
            file.name.substring(file.name.lastIndexOf('.')).toLowerCase()
          ) > 0
        ) {
          isLtSize = file.size > 1024 * 1024 * 50
          isVideo = true
        }
        if (isLtSize) {
          if (this.hasVideo && isVideo) {
            this.$message.error(`${typeName}不能超过50M哦`)
          } else {
            this.$message.error(`${typeName}不能超过${sizeStr}`)
          }
          return false
        }
      }
    },
    handleRemove(file, fileList) {
      this.$emit('handleRemove', file, fileList)
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值