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

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

async await new Promise

javascript 复制代码
    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);
          });

      })


    },
相关推荐
LuckyLay6 分钟前
React百日学习计划-Grok3
前端·学习·react.js
澄江静如练_10 分钟前
小程序 存存上下滑动的页面
前端·javascript·vue.js
源码方舟17 分钟前
基于SpringBoot+Vue的房屋租赁管理系统源码包(完整版)开发实战
vue.js·spring boot·后端
互联网搬砖老肖26 分钟前
Web 架构之会话保持深度解析
前端·架构
m0_5139625329 分钟前
vue-ganttastic甘特图label标签横向滚动固定方法
javascript·vue.js·甘特图
菜鸟una33 分钟前
【taro3 + vue3 + webpack4】在微信小程序中的请求封装及使用
前端·vue.js·微信小程序·小程序·typescript·taro
Java&Develop39 分钟前
怎么查看当前vue项目,要求的node.js版本
vue.js
hao_041343 分钟前
elpis-core: 基于 Koa 实现 web 服务引擎架构设计解析
前端
松树戈1 小时前
openfeign与dubbo调用下载excel实践
vue.js·spring cloud·elementui·dubbo
码农黛兮_461 小时前
HTML、CSS 和 JavaScript 基础知识点
javascript·css·html