6

I'm trying to append uploaded file and a data value to FormData in vue.js. In my controller, only the files request can be assessed.

data() {
  return (
    file: '',
    categ: ''
  }
}

And in my method:

var form = new FormData();
var file = this.file;
var cat = this.categ;
form.append('pics', file, cat);

axios.post('/api', form, { headers:
  { 'Content-Type': 'multipart/form-data' }
}).then (res => console.log(res));  

What am I doing wrong?

1
  • 1
    In form.append('pics', file, cat);, the third argument, cat is the name of the file. Commented Mar 31, 2018 at 16:31

2 Answers 2

20

The problem is how you are probably getting the file from the input.

If your input looks like this:

<input type="file"  @change="upload($event)" id="file-input">

Then you see an use the $event to get the file and use it as:

methods: {
  upload(event){
    let data = new FormData();
    let file = event.target.files[0];

    data.append('name', 'my-file')
    data.append('file', file)

    let config = {
      header : {
       'Content-Type' : 'multipart/form-data'
     }
    }

    axios.post('/api', data, config).then(
      response => {

      }
    )
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @samayo..I was actually able to access the file in my backend. However, there's another data value which is named 'cat' I need to send to the backend along with the files. Do I append it same way like u did the 'name' & 'file'?
Didn't know append accepted file name also. I guess you can do it in one append like like in your code. But cat should be a filename
You can remove the $event from @change="upload($event)". If you are not passing anything, the method will capture the event by default in the first parameter.
ah yeah good one, thanks. i will leave it as is though, it less confusing to new comers. what do you think?
2

Here is solution for multiply files uploading. Don't forget to add unique id for every formData object.

<input type="file" multiple :disabled="isSaving" @change="filesChange($event.target.files)" accept="your_format_here" class="input-file">

And here is axios method and creating of our formData object.

  methods: {
    async save(datafile) {
      // upload data to the server
      return await axios.post(
          store.state.endpoints.baseURL + "upload/url/",
          datafile, {
            headers: {
              Authorization: 'Bearer ' + store.state.jwt
            }
          }
        )
        .then(response => {
          console.log(response)
        })
        .catch(err => {
          console.log(err.response)
        });
    },
    filesChange(fileList) {
      const formData = new FormData();
      if (!fileList.length) return;
      for (var i = 0; i < fileList.length; i++) {
        let file = fileList[i];
        // Here we create unique key 'files[i]' in our response dict
        formData.append('files[' + i + ']', file);
      }
      this.save(formData);
    }
  },

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.