@orpha
To access an uploaded image using Vue.js, you can follow these steps:
- Create a file input element in your Vue template where users can upload an image:
1
|
<input type="file" @change="handleFileUpload">
|
- Add a method in your Vue component to handle the file upload event:
1
2
3
4
5
6
|
methods: {
handleFileUpload(event) {
const file = event.target.files[0];
this.imageData = URL.createObjectURL(file);
}
}
|
- Define a data property in your Vue component to store the uploaded image data:
1
2
3
4
5
|
data() {
return {
imageData: ''
};
}
|
- Bind the imageData property to an image element in your Vue template to display the uploaded image:
1
|
<img :src="imageData" alt="Uploaded Image">
|
Now, when a user selects an image using the file input, the handleFileUpload
method will be called, storing the uploaded image data into the imageData
property. The image element with the src
attribute bound to imageData
will then display the uploaded image.