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);
          });

      })


    },
相关推荐
小小小小宇31 分钟前
Vue.nextTick()笔记
前端
小约翰仓鼠2 小时前
vue3子组件获取并修改父组件的值
前端·javascript·vue.js
Lin Hsüeh-ch'in2 小时前
Vue 学习路线图(从零到实战)
前端·vue.js·学习
烛阴2 小时前
bignumber.js深度解析:驾驭任意精度计算的终极武器
前端·javascript·后端
计蒙不吃鱼2 小时前
一篇文章实现Android图片拼接并保存至相册
android·java·前端
全职计算机毕业设计3 小时前
基于Java Web的校园失物招领平台设计与实现
java·开发语言·前端
你的人类朋友3 小时前
✍️Node.js CMS框架概述:Directus与Strapi详解
javascript·后端·node.js
啊~哈3 小时前
vue3+elementplus表格表头加图标及文字提示
前端·javascript·vue.js
xiaogg36783 小时前
vue+elementui 网站首页顶部菜单上下布局
javascript·vue.js·elementui