How to async/await multiple uploads before final form submit?

安稳与你 提交于 2021-01-29 05:14:24

问题


I am trying to allow users to upload multiple files. Each file selected will trigger an axios.post() request... so if a user selects three files then there will be three separate axios.post() requests made to the server to upload the files.

There are also other fields in the form such as Name and Description which will be submitted when the user clicks the form submit button. However, I want the form submission to wait until ALL the files are uploaded first.

Here is a shortened version of the code for brevity:

<form @submit.prevent="sendForm" enctype="multipart/form-data">
  <input multiple ref="PostFiles" name="PostFiles" type="file" @change="selectFile">
  ... // other fields here
  <button type="submit" name="Send" value="Send"></button>
</form>


methods: {


     selectFile() {
               const this.Form.UploadFiles = this.$refs.PostFiles.files; // all files selected by user to upload

               Array.from(this.Form.UploadFiles).forEach(file => {
               this.uploadFile(file) // each file gets sent to uploadFile() individually
               });
     },

     async uploadFile(File) {
                const FormFile = new FormData();
                FormFile.append("PostFile", File);

                await this.$axios.post('/api', FormFile).then(response => {
                    console.log("Successfully uploaded")
                }).catch(err => {
                    console.log(err.response.data.error)
                })
                return true;
      },

      async sendForm() {
            const FormBody = new FormData();
            FormBody.append("Name", this.Form.Name);
            FormBody.append("Description", this.Form.Description);
            // Here I need to wait for all files to upload first!
            await this.uploadFile();
            // If all files uploaded then post the form
            await this.$axios.post('/api', FormBody)
      }

Because uploadFile() returns true after each individual upload, the sendForm() will submit after the first file is uploaded. But I need to await until ALL files in this.Form.UploadFiles array are uploaded. How could I do that?


回答1:


Try it with this approach:

 async selectFile() {
           const this.Form.UploadFiles = this.$refs.PostFiles.files; // all files selected by user to upload

           let allFiles = Array.from(this.Form.UploadFiles);
           for(let i = 0; i < allFiles.length; i++){
              console.log("Uploaded File number: " + i + 1);
              await this.uploadFile(allFiles[i])
           }

 },

 uploadFile(File) {
            const FormFile = new FormData();
            FormFile.append("PostFile", File);
            return this.$axios.post('/api', FormFile);
  },

  async sendForm() {
        const FormBody = new FormData();
        FormBody.append("Name", this.Form.Name);
        FormBody.append("Description", this.Form.Description);
        // Here I need to wait for all files to upload first!
        this.selectFile();
        // If all files uploaded then post the form
        await this.$axios.post('/api', FormBody)
  }



回答2:


You can make selectFiles as async and use a for Loop instead of forEach()

async selectFile() {
    const this.Form.UploadFiles = this.$refs.PostFiles.files; // all files selected by user to upload

    for (const eachFile of Array.from(this.Form.UploadFiles)) {
        await this.uploadFile(eachFile); // each file gets sent to uploadFile() individually
    }
}



async sendForm() {
        const FormBody = new FormData();
        FormBody.append("Name", this.Form.Name);
        FormBody.append("Description", this.Form.Description);
        // Here I need to wait for all files to upload first!
        await this.selectFile();   //<--this is the change you need
        // If all files uploaded then post the form
        await this.$axios.post('/api', FormBody)
  }


来源:https://stackoverflow.com/questions/61676984/how-to-async-await-multiple-uploads-before-final-form-submit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!