el-upload批量上传,获取第一个数据返回参数并在后面的上传中进行传递;递归

发布时间 2023-12-23 14:16:09作者: 站住,野猪佩奇
fileChange(file, fileList) {
  const { raw } = file;
  if (file.status != 'ready') return;
  if (file.size / 1024 / 1024 > 100) {
    this.$message.error('文件大小不能超过100M');
    return false;
  }
 
  this.upFileList = []
  for (let x of fileList) {
    this.upFileList.push(x.raw)
  }
 
  clearTimeout(this.time)
  this.time = setTimeout(() => {
    this.time = null
  this.submitUpload2()
  }, 1000)
},
 
submitUpload2 () {
  if (this.upFileList.length > 0) {
  
    this.fileNum = new FormData()
    this.fileNum.append('attach', this.upFileList[0])

    if (!this.onlyDocId) {
      uploadDoc(this.fileNum).then(res => {
      // 每次上传当前一个后 不论成功失败就删除当前这个
      // this.upFileList.shift()
      // console.log('上传返回', res)
      if (res.success) {
        this.onlyDocId = res.result.id
        this.fileList.push({ name: this.upFileList[0].name, docId: res.result.id })
        console.log(this.fileList, 'fileList')
        this.upFileList.shift()
        this.submitUpload2()
      }
     })
    } else {
      uploadDoc1(this.onlyDocId, this.fileNum).then(res => {
        // this.upFileList.shift()
        // console.log('上传返回', res)
        if (res.success) {
          this.fileList.push({ name: this.upFileList[0].name, docId: res.result.id })
          console.log(this.fileList, 'fileList')
          this.upFileList.shift()
          this.submitUpload2()
        }
      })
    }
  }
},