Kurt_D_Cobain,
function handleFiles(files) {
initializeProgress(files.length)
for (let i = 0; i < files.length; i++) {
document.getElementById('gallery').appendChild(document.createElement('div'))
};
files = [...files];
files.forEach(uploadFile)
files.forEach(previewFile)
files.forEach(previewName)
files.forEach(previewSize)
files.forEach(removeFile)
}
function previewFile(file, i) {
let reader = new FileReader()
reader.readAsDataURL(file)
reader.onloadend = function () {
let img = document.createElement('img')
img.src = reader.result
document.querySelectorAll("#gallery > div")[i].appendChild(img)
}
}
function previewName(file, i) {
let reader = new FileReader()
reader.readAsDataURL(file)
reader.fileName = file.name
reader.onloadend = function () {
let title = document.createElement('span')
title.innerHTML = reader.fileName
document.querySelectorAll("#gallery > div")[i].appendChild(title)
}
}
function previewSize(file, i) {
let reader = new FileReader()
reader.readAsDataURL(file)
reader.fileSize = formatBytes(file.size)
function formatBytes(bytes,decimals) {
if(bytes == 0) return '0 Bytes';
var k = 1024,
dm = decimals <= 0 ? 0 : decimals || 2,
sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
reader.onloadend = function() {
let size = document.createElement('div')
size.innerHTML = reader.fileSize
document.querySelectorAll("#gallery > div")[i].appendChild(size)
}
}
function removeFile(file, i) {
let reader = new FileReader()
reader.readAsDataURL(file)
reader.onloadend = function() {
let remove = document.createElement('span')
remove.setAttribute('id','RemoveFile')
remove.innerHTML = 'x'
document.querySelectorAll("#gallery > div")[i].appendChild(title)
}
}