I was looking for a solution though there are many answers on the same topics I was unable to figure out the problem in my code.
The problem is, I can only read the first file input using this API.
The last two inputs show undefined if I console.log(e.target.files[1]);
I am using vue 2. Here is the codes I have.
For the three inputs I have
<input type="file" name="file[]" @change="img1">
<input type="file" name="file[]" @change="img2">
<input type="file" name="file[]" @change="img3">
img1(e){
console.log(e.target.files[0]);
this.readFile(e,'img1',0);
},
img2(e){
console.log(e.target.files[1]);
this.readFile(e,'img2',1);
},
img3(e){
console.log(e.target.files[2]);
this.readFile(e,'img3',2);
},
Here is my readFile method
readFile(e,img,i) {
let self=this;
if(window.FileReader) {
var file = e.target.files[i];
var reader = new FileReader();
if (file) {
let type=e.target.files[i].type;
console.log(type);
if(!this.cheackType(type)){
this.showTypeWarn('Invalid Image Formate', 'Please Upload jpg or a png file only');
return
}
reader.readAsDataURL(file);
} else {
// img.css('display', 'none');
// img.attr('src', '');
}
reader.onloadend = function (e) {
self.data.img=reader.result;
}
}
}
Thank you.
e.target.nextElementSibling.files[0]. usingidwould be simplest