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.
Thanks for your feedback!
Your contributions will help us to improve service.
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
In this JavaScript code:
-
We initialize a new Vue instance and specify
#appas the element to mount it to. -
We set up Vuetify by creating a new instance of Vuetify.
-
Inside the
dataproperty, we define aselectedFilevariable with an initial value ofnull. This variable will be used to store the selected file. -
The
watchproperty is used to watch for changes in theselectedFilevariable. When a new file is selected, theselectedFilefunction is called. -
Inside the
selectedFilefunction, we check if a new file has been selected (i.e.,newFileis notnull), 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
Output of

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.