Input of type file to obtain value

2

I have a problem getting the value of my input :

<input type="file" name="fileToUpload[]" id="inputFileServer" accept="image/png"/>

In angular I have:

var file = document.getElementById('inputFileServer').files[0];
console.log("File: ", file);

Retrieving " undefined "

    
asked by jecorrales 25.09.2017 в 21:38
source

1 answer

1

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();" />
    
answered by 25.09.2017 / 22:24
source