The result is as expected. If, when loading the page, no file was selected in its input
, it will not be possible to obtain the item with index 0
since the FileList
will be empty.
To notice the change, you could place the code Js
in a function in the event change
function cambiarFile(){
const input = document.getElementById('inputFileServer');
if(input.files && input.files[0])
console.log("File Seleccionado : ", input.files[0]);
}
console.log("Sin Archivo Seleccionado " + document.getElementById('inputFileServer').files[0]);
<input type="file" name="fileToUpload" id="inputFileServer" accept="image/png" onchange="return cambiarFile();" />