Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

v-model doesn't support input type="file"

I can not use v-model with file input, Vue says I must use v-on:change. Ok so I can use v-on:change, but how can I bind the "content" of the input file to a data property?

Let's say I want to bind it in a component to this.file:

export default {   data() {     file: null   },   // ... } 

Here is the HTML part:

<input id="image" v-on:change="???" type="file"> <!--                           ^- don't know how to bind without v-model --> 

How should I do the binding?

like image 720
rap-2-h Avatar asked Jan 23 '17 09:01

rap-2-h


People also ask

What is input V-model?

Vue v-model is a directive that creates a two-way data binding between a value in our template and a value in our data properties. A common use case for using v-model is when designing forms and inputs. We can use it to have our DOM input elements be able to modify the data in our Vue instance.

What is input file format?

The file input type is used to identify resource(s) in the file structure, upload a file, or create a resource to upload. The file input element provides a GUI to select a file, and then represents a list of files that are selected with the option to change resource(s) until form submission.

How do you bind a V-model?

To bind the value of an input element to a property of your component's data, use v-model="dataProperty" like so. Notice the input value starts out as Am I truly an alligator? , but when you change the input, the existentialQuestion property (and the h2 element) will update in real time.

Is it possible to use input type file with V-model?

Note: v-model only works with input which use value binding, where input type file doesn't use input value, it makes no sense to use input type file with v-model Thanks for contributing an answer to Stack Overflow!

How do I add V-model support to a custom component?

To support v-model, the component accepts a value prop and emits an input event. Use the component like this: With that, the custom component supports v-model two-way binding. Let’s take it a step further. We might not want to use the default event and prop needed to add v-model support to our components.

Is the model value going to be file's path instead of name?

The thing is, the way things are set up today, the model value will be file's path instead of file name (which I think is what @airtoxin expected). Take a look at this snippet. What do you guys think? Is this the expected behavior? Sorry, something went wrong. @rafaelrinaldi hi, thanks to your examples.

Does the Custom component support V-model two-way binding?

With that, the custom component supports v-model two-way binding. Let’s take it a step further. We might not want to use the default event and prop needed to add v-model support to our components. Thankfully, Vue allows us to customize it.


2 Answers

In the onchange event you should pass the event object to a function and handle:

onFileChange(e) {   var files = e.target.files || e.dataTransfer.files;   if (!files.length)     return;   this.createImage(files[0]); }, 

For more information refer https://codepen.io/Atinux/pen/qOvawK/

like image 80
Arun Ghosh Avatar answered Oct 04 '22 06:10

Arun Ghosh


Using v-model with a file input makes no sense, because you can't set a value on a file input - so what should a two-way binding do here?

Just use v-on:change

like image 43
Linus Borg Avatar answered Oct 04 '22 08:10

Linus Borg