vue循环发起请求,等一个请求结束后,进行下一次请求

22 篇文章 0 订阅
文章讲述了在Vue应用中,如何使用async/await和Promise实现文件的并发上传,处理文件列表中同名文件,以及在上传过程中进行异步操作和错误处理的过程。
摘要由CSDN通过智能技术生成

vue循环发起请求,等一个请求结束后,进行下一次请求
async await new Promise

    async filesSubmitted(files, fileList) {
      if (files.length === 0) {
        return this.$message.error("文件列表存在同名文件,请关闭文件列表后再试。");
      }

      for (let file of files) {
        const path = this.filePath
        const name = file.relativePath
        const size = file.size
        var formData = new FormData();
        formData.append('concurrentType', this.fileType)
        formData.append('fileName', path + name)
        formData.append('partCount', Math.ceil(size / this.chunkSize))
        formData.append('fileSize', size)
        formData.append('userId',this.createUser)
        formData.append('type',1)
        if (this.fileSetCode) {
          formData.append('datasetCode', this.fileSetCode)
        }
        let res = await this.submitFile(formData, file)
      }
      this.isAllCover = -1
    },
    // 文件提交
    submitFile(formData, file) {
      return new Promise((resolve, reject) => {
        this.axios.post(api['distributed'].uploadTestFile, formData
        ).then(response => {
          let res = response.data
          if (res.code === 200) {
            setTimeout(() => {
              file.chunkUrlData = res.data.ulist
              file.chunkUrlData.uploadId = res.data.uploadId
              file.path = res.data.fileName
              var code = res.data.paperCode + '/' 
              if (this.menuItem.length == 0) {
                this.filePath = '论文/' + code
              } else {
                this.filePath = this.menuItem[this.menuItem.length - 1].path
              }
              this.dataSetCode = code
              this.$emit('getCurrentDataSetCode', code)
              // 存在重复文件
              if (res.data.isExist) {
                if (this.isAllCover === 1) { // 已选择全部替换
                  this.startUpload(file, false)
                  resolve()
                } else if (this.isAllCover === -1) { // 询问是否需要全部替换
                  this.$confirm('存在重复文件, 是否全部替换?', '提示', {
                    confirmButtonText: '全部替换',
                    cancelButtonText: '逐一替换',
                    type: 'warning'
                  }).then(() => {
                    this.isAllCover = 1
                    this.startUpload(file, false)
                    resolve()
                  }).catch(() => {
                    this.isAllCover = 0 // 选择逐一替换
                    this.$confirm(file.name + '已存在, 是否选择替换?', '提示', {
                      confirmButtonText: '确定',
                      cancelButtonText: '取消',
                      type: 'warning'
                    }).then(() => {
                      this.startUpload(file, false)
                      resolve()
                    }).catch(() => {
                      this.startUpload(file, true)
                      resolve()
                    });
                  });
                } else if (this.isAllCover === 0) {
                  this.$confirm(file.name + '已存在, 是否选择替换?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                  }).then(() => {
                    this.startUpload(file, false)
                    resolve()
                  }).catch(() => {
                    this.startUpload(file, true)
                    resolve()
                  });
                }
                // this.startUpload(file, res.data.isExist)
              } else {
                this.startUpload(file, res.data.isExist)
                this.$emit('deletePath', res.data.fileName)
                resolve()
              }

            }, 1000);
          } else {

          }
        })
          .catch(function (error) {
            console.log(error);
          });

      })


    },
  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值