Vuetify input File get Selected file name

In this tutorial, we will explore how to create a Vuetify file input component and use JavaScript to get the name of the selected file. We'll walk you through the code, explaining each part step by step.

Profile Photo

Abhishek Yadav (SD) SDE of FAI

Feedback

written
Profile Photo

Anil Kumar (Expert) Coding Expert of FAI

Feeback

reviewed
Profile Photo
Oct 31, 2023 06:10 AM Last Updated
updated

How to Get the Selected File Name with Vuetify File Input in JavaScript?

In this HTML code, we create a Vuetify container and a file input component. The v-model directive binds the input's value to the selectedFile variable. We also display the selected file's name or "No file selected" using Vue's templating syntax

HTML Code:

Copied to Clipboard
Run

In this JavaScript code:

  1. We initialize a new Vue instance and specify #app as the element to mount it to.

  2. We set up Vuetify by creating a new instance of Vuetify.

  3. Inside the data property, we define a selectedFile variable with an initial value of null. This variable will be used to store the selected file.

  4. The watch property is used to watch for changes in the selectedFile variable. When a new file is selected, the selectedFile function is called.

  5. Inside the selectedFile function, we check if a new file has been selected (i.e., newFile is not null), and if so, we log the selected file's information to the console.

The key to understanding this code is the v-model binding, which automatically updates the selectedFile variable whenever a file is selected. The watch block then detects this change and logs the selected file's information

Javascript Code

Copied to Clipboard

Output of 

Pasted Image

In summary, this code demonstrates how to create a Vuetify file input component and use Vue.js to dynamically display the selected file's name. The watch function helps monitor changes in the selectedFile variable, allowing you to perform actions based on the selected file, such as logging its information to the console. This is a simple and effective way to work with file inputs in Vuetify and Vue.js.

Ad