$('#element-to-get-file').change(function (e) {
//inputFile is the input with type = file
var curFile = inputFile.files;
//check if the file is attached
if (curFile.length === 0) {
//show error
textTo.textContent = 'No files currently selected for upload';
} else {
var i = 0;
if (validFileType(curFile[i])) {
console.log('file is found and in a stated file type!');
imgTo.src = window.URL.createObjectURL(curFile[i]);
//$(imgTo).attr('src', curFile[i]);
//var img = document.createElement('img');
//img.src = window.URL.createObjectURL(curFile[i]);
//$('body').append(img);
} else {
para.textContent = 'File name ' + curFile[i].name + ': Not a valid file type. Update your selection.';
textTo.textContent = 'not found the file in the length';
}
}
});
var fileTypes = [
'image/jpeg',
'image/pjpeg',
'image/png'
]
function validFileType(file) {
for (var i = 0; i < fileTypes.length; i++) {
if (file.type === fileTypes[i]) {
return true;
}
}
return false;
}