el-upload上传文件 需要在请求之前加一个校验文件内容格式请求

发布时间 2023-10-09 16:40:26作者: 醒日是归时

before-upload

    data(){
      return {
        ...
        //判断是否需要做文件检查
        checkFileFormat:false
      }
    },
    beforeUpload(rawFile) {
      this.loading = true;
      ...
      检查文件格式
      检查文件大小
      ...

      if (this.checkFileFormat) {
        return new Promise(async (resolve, reject) => {
          const fd = new FormData()
          fd.append('file', rawFile)
          const res = await checkRpAttFile(fd)
          this.loading = false;
          if (!res.success) {
            Message.error(res.message);
            reject()
          } else {
            resolve()
          }
        })
      } else {
        return true
      }

    },