Vue js image upload preview; Through this tutorial, i am going to guide you how show image preview before upload in vue js.
Vue JS Image Upload with Preview
Just follow the following steps and display image preview before upload in vue Js app:
- Step 1 – Create New VUE JS App
- Step 2 – Create Image Preview Component
- Step 3 – Import Component on App.vue
Step 1 – Create New VUE JS App
In this step, open your terminal and execute the following command to create new vue js app:
vue create vue-image-upload-preview
Step 2 – Create Image Preview Component
Go to /src/components directory and create a new component called file-preview.vue and add the following code into it:
<template> <div> <div class="imagePreviewWrapper" :style="{ 'background-image': `url(${previewImage})` }" @click="selectImage"> </div> <input ref="fileInput" type="file" @input="pickFile"> </div> </template> <script> export default { data() { return { previewImage: null }; }, methods: { selectImage () { this.$refs.fileInput.click() }, pickFile () { let input = this.$refs.fileInput let file = input.files if (file && file[0]) { let reader = new FileReader reader.onload = e => { this.previewImage = e.target.result } reader.readAsDataURL(file[0]) this.$emit('input', file[0]) } } } } </script> <style scoped lang="scss"> .imagePreviewWrapper { width: 250px; height: 250px; display: block; cursor: pointer; margin: 0 auto 30px; background-size: cover; background-position: center center; } </style>
Step 3 – Import Component on App.vue
Go to /src/ directory and App.vue file. And then add the following code into it:
<template> <FilePreview></FilePreview> </template> <script> import FilePreviewfrom './components/FilePreview'; export default { components: { FilePreview } } </script>
Conclusion
Vue js image upload with preview example. In this tutorial, you have learned how display image preview before upload in vue js.
Recommended VUE JS Tutorials
Recommended:-Vue Js For Loop (v-for) Example
Recommended:-Vue JS Ajax Form Submit Tutorial
Recommended:-Vue Js Add Class To Element On Click Tutorial
Recommended:-Vue JS Checkbox Checked Event Tutorial
Recommended:-Vue Js Get Current Date and Time
Recommended:-How to Add and Use Bootstrap Modal in Vue Js App
Recommended:-Vue JS Download File using Axios App
Recommended:-Vue Js keydown Event Example
Recommended:-Vue.Js in Reverse Array Using v-for OrderBy Filter
Recommended:-Vue JS Radio Button OnChange Event
Recommended:-Vue JS Get Query String Parameters
Recommended:-Vue Js Google Area Chart Tutorial
Recommended:-Vue Js Get Selected Value from Selected Dropdown
Recommended:-Vue Js Remove Element From Array
Recommended:-Vue Js Google Pie Chart Tutorial
Recommended:-How to Create Laravel 8 Vue JS Like Dislike System
Recommended:-Laravel 8 Vue JS Image/File Upload Tutorial Example