I want to make a image preview when upload images.
I was able to get the path of image in an array, but the array does not update the first time when I upload an image.
When check the vue devtools, it updates the array but the image doesn't preview.
When I upload images, the array should updated immediately. But it doesn't update.
If I view my array with the vue devtools, the array does update.
Here is my code:
html
<div class='row'>
<div v-for="item in itemsImages">{{item}}</div>
</div>
script
export default {
data() {
return {
items: [],
itemsAdded: '',
itemsNames: [],
itemsSizes: [],
itemsImages: [],
itemsTotalSize: '',
formData: '',
},
methods: {
onChange(e) {
this.successMsg = '';
this.errorMsg = '';
this.formData = new FormData();
let files = e.target.files || e.dataTransfer.files;
this.itemsAdded = files.length;
let fileSizes = 0;
var vm = this;
for (let x in files) {
if (!isNaN(x)) {
this.items = e.target.files[x] ||
e.dataTransfer.files[x];
this.itemsNames[x] = files[x].name;
this.itemsSizes[x] = this.bytesToSize(files[x].size);
fileSizes += files[x].size;
var reader = new FileReader();
reader.onload = (event) => {
vm.itemsImages[x] = event.target.result;
};
reader.readAsDataURL(files[x]);
this.formData.append('items[]', this.items);
}
}
this.itemsTotalSize = this.bytesToSize(fileSizes);
},
},
}