[Vuejs]-Set parameter in FormData for dynamic fields (Javascript)

0👍

You’re correctly using .append instead of .set, but you forgot that variables should be arrays, thus their keys should be file[], name[], comment[] and assignment_solution[] respectively.

This way your backend will correctly recognize that they are indeed arrays.

0👍

You are appending multiple files using the same name. Only the last addition will reach your server.

You have at least 2 options:

  1. Give unique names to each field. Something similar to this

    formdata.append(‘file’ + i, this.$refs.assignmentFile[i].files[0]);

Notice the concatenation of file with the index variable.

  1. Use the array notation for the fields’ names. This is compatible with how PHP handled POST variables.

    formdata.append(‘file[]’, this.$refs.assignmentFile[i].files[0]);

Notice the square brackets in the name file[].

You need to do this for all the fields: file, name, comments.

Leave a comment