Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File input on change in vue.js

Using plain HTML/JS, it is possible to view the JavaScript File objects of selected files for an input element like so:

<input type="file" id="input" multiple onchange="handleFiles(this.files)"> 

However, when converting it to the 'Vue' way it doesn't seem to work as intend and simply returns undefined instead of returning an Array of File objects.

This is how it looks in my Vue template:

<input type="file" id="file" class="custom-file-input"    v-on:change="previewFiles(this.files)" multiple> 

Where the previewFiles function is simply the following (located in methods):

  methods: {     previewFiles: function(files) {       console.log(files)     }   } 

Is there an alternate/correct way of doing this? Thanks

like image 214
Ash Avatar asked Jul 19 '17 00:07

Ash


People also ask

How do I get input value from Vue?

The v-model directive in Vue creates a two-way binding on the input element. All you need to do is simply declare a v-model directive for the input element and reference it to get the value. Every time the input value changes, “myInput” model will keep track of the changes.

How do you assign a value to a file?

The only way to set the value of a file input is by the user to select a file. This is done for security reasons. Otherwise you would be able to create a JavaScript that automatically uploads a specific file from the client's computer.

What is a .Vue file?

VUE file extension is mainly associated with single file components used by Vue, an open-source, frontend JavaScript framework for building user interfaces. Such VUE single-file components are modules containing source code (typically for a single UI component) which can be exported or reused in a Vue application.


1 Answers

Another solution:

<input type="file" @change="previewFiles" multiple>  methods: {    previewFiles(event) {       console.log(event.target.files);    } } 
like image 121
oboshto Avatar answered Sep 20 '22 13:09

oboshto