3

How to take data from v-model array in input type="file" multiple ?

<input type="file" multiple v-model="modFiles[index]">
<input type="file" multiple v-model="modFiles[index]">
<input type="file" multiple v-model="modFiles[index]">

I'm using v-for loop and I can get the first data from each modFiles[].

this.modFiles[0] //take the first from multiple file

But it's only the first data. How can I take all the data inside modFiles[0],modFiles[1],modFiles[3] ? And how to count the data inside each modFiles ?

this.modFiles[0].length //i get error here

thanks so much

1 Answer 1

5

Bidirectional binding is not supported for <input type="file">, since you're not allowed to set value on such inputs (value is only set after user chooses a file).

Use @change event instead:

<input type="file" multiple @change="handleFileChange($event, index)">

methods: {
  handleFileChange(evt, index) {
    // evt.target.files contains Array-like FileList object
  }
}

Update:

In order to show/hide your submit button based on count of selected files, introduce new data property:

data: {
  filesSelected: 0
},
methods: {
  handleFileChange(evt, index) {
    this.filesSelected = evt.target.files.length;
  }
}

And then use it in your template:

<input type="submit" v-show="filesSelected > 0" />
Sign up to request clarification or add additional context in comments.

1 Comment

what can i do if i want to make submit button doesn't appear before the user select some file? and the submit button appear after file has been selected

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.