简体   繁体   English

使用 vuejs 或 vuex 提交表单时,formdata.append 不起作用

[英]formdata.append not working when submiting a form using vuejs or vuex

I do have this form and i have another component that use vue2-dropzone to upload files.我有这个表格,我有另一个使用 vue2-dropzone 上传文件的组件。 When choosing the files the $store state uploads will be updated.选择文件时,将更新 $store state 上传。 My issue is when am submiting the form so the FormData not sending the files.我的问题是提交表单时 FormData 不发送文件。 formdata.append not working. formdata.append 不工作。 I do check under.networks and i can see that formdata is equal to {}.我确实检查了 under.networks,我可以看到 formdata 等于 {}。 What i did for wrong please.请问我做错了什么。

<form class="outer-repeater" @submit.prevent="submitTask">
              <div data-repeater-list="outer-group" class="outer">
                <div data-repeater-item class="outer">
                  <div class="form-group row mb-4">
                    <label for="taskname" class="col-form-label col-lg-2">Task Name</label>
                    <div class="col-lg-10">
                      <input
                          id="taskname"
                          name="taskname"
                          type="text"
                          class="form-control"
                          placeholder="Enter Task Name..."
                          v-model="task.title"
                      />
                    </div>
                  </div>
                  <div class="form-group row mb-4">
                    <label class="col-form-label col-lg-2">Task Description</label>
                    <div class="col-lg-10">
                      <ckeditor :editor="editor" v-model="task.description"></ckeditor>
                    </div>
                  </div>
    
                  <div class="form-group row mb-4">
                    <label class="col-form-label col-lg-2">Task Date</label>
                    <div class="col-lg-10">
                      <date-picker v-model="task.daterange" range append-to-body lang="en" confirm></date-picker>
                    </div>
                  </div>
    
                  <div class="inner-repeater mb-4">
                    <div class="inner form-group mb-0 row">
                      <label class="col-form-label col-lg-2">Add Team Member</label>
                      <div
                          class="inner col-lg-10 ml-md-auto"
                      >
                        <div class="mb-3 row align-items-center">
                          <div class="col-md-12">
                            <multiselect v-model="task.teamMember" label="name" :options="options" :multiple="true"
                                         :taggable="true"></multiselect>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                  <div class="form-group row mb-4">
                    <label for="taskbudget" class="col-form-label col-lg-2">Budget</label>
                    <div class="col-lg-10">
                      <input
                          id="taskbudget"
                          name="taskbudget"
                          type="text"
                          placeholder="Enter Task Budget..."
                          class="form-control"
                          v-model="task.budget"
                      />
                    </div>
                  </div>
                  <div class="form-group row mb-4">
                    <label for="taskbudget" class="col-form-label col-lg-2">Ladda up</label>
                    <div class="col-lg-10">
                      <uploads/>
                    </div>
                  </div>
    
                </div>
              </div>
              <div class="row justify-content-end">
                <div class="col-lg-10">
                  <button type="submit" class="btn btn-primary" >Create Task</button>
                </div>
              </div>
            </form>

  methods: {
 
    files(){
      return this.$store.state.uploads.uploads
    },
 
    submitTask() {
      let formData = new FormData();
      for (var i = 0; i < this.files.length; i++) {
        let file = this.files[i];
        formData.append('files[' + i + ']', file);
        console.log(file)
      }
      formData.append('task',this.task)
      axios.post('http://test.local/wp-json/mytestapi/submitTodo',
          {
            formData,
            headers: {
              'Content-Type': 'multipart/form-data;boundary=' + Math.random().toString().substr(2),
            }
          }).then(res => {
        console.log(res.data)
      }).catch(res => {
        console.log(res)
      })

    },

  },

在此处输入图像描述

If you want multiple files in an array like that, you have to append the exact field name each time.如果您想要像这样的数组中的多个文件,则每次都必须 append 准确的字段名称。 Don't add the index and you don't need the brackets:不要添加索引,也不需要括号:

formData.append('files[' + i + ']', file); // ❌ Wrong
formData.append('files', file); // ✅ Correct, use the same name each time

When you axios.post , the formData should be the 2nd argument:当你axios.post时, formData应该是第二个参数:

const url = 'http://test.local/wp-json/mytestapi/submitTodo';
axios.post(url, formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM