This is my code :
var fr = new FileReader();
var readFiles;
var fileI;
var files;
fr.onload = () => {
readFiles.push(fr.result);
fileI ++;
if (fileI < files.length) {
fr.readAsDataURL(files[fileI]);
}
};
function getVideoImage(path, secs, callback) {
var me = this, video = document.createElement('video');
video.onloadedmetadata = function() {
if ('function' === typeof secs) {
secs = secs(this.duration);
}
this.currentTime = Math.min(Math.max(0, (secs < 0 ? this.duration : 0) + secs), this.duration);
};
video.onseeked = function(e) {
var canvas = document.createElement('canvas');
canvas.height = video.videoHeight;
canvas.width = video.videoWidth;
var ctx = canvas.getContext('2d');
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
var img = new Image();
img.src = canvas.toDataURL();
callback.call(me, img, this.currentTime, e);
};
video.onerror = function(e) {
callback.call(me, undefined, undefined, e);
};
video.src = path;
}
$uploadFiles.on("input", () => {
files = $uploadFiles[0].files;
var value = $uploadFiles[0].value;
fileI = 0;
readFiles = [];
fr.readAsDataURL(files[0]);
for (var i = 0; i < files.length; i++) {
var format = files[i].name.split(".").pop().toUpperCase();
var thumbnail;
getVideoImage(readFiles[i], 0, (img) => {
thumbnail = img;
});
if (fileFormat.video.includes(format)) {
propertiesData.FILES.contents[0].push({
thumbnail: thumbnail,
title: files[i].name,
file: readFiles[i]
});
} else if (fileFormat.image.includes(format)) {
console.log(readFiles);
console.log(i);
console.log(readFiles[i])
propertiesData.FILES.contents[1].push({
thumbnail: readFiles[i],
title: files[i].name,
file: readFiles[i]
});
} else if (fileFormat.audio.includes(format)) {
propertiesData.FILES.contents[2].push({
thumbnail: "Assets/Logo.PNG",
title: files[i].name,
file: readFiles[i]
});
} else {
alert("File Format Not Supported");
}
$("#properties-left-menu li[style='color: ${color[1]}']")
}
});
It reads the uploaded files and saves the raw files in data object(propertiesData). The problem is this part :
console.log(readFiles);
console.log(i);
console.log(readFiles[i])
propertiesData.FILES.contents[1].push({
thumbnail: readFiles[i],
title: files[i].name,
file: readFiles[i]
});
console.log(readFiles); shows array of raw files like this and console.log(i); shows the proper index so console.log(readFiles[i]); is supposed to show one of the raw file string, but it's showing only undefined. Why?