Turtle Turtle - 6 months ago 9
Javascript Question

Accessing value of javascript file input variable

I am trying to get the filepath of the selected file.
Using the debugger, I see that the file has an property called value which is equal to :

"C:\fakepath\filename.txt"
.


However when I try to access file.value, the filepath is equal to null.
I am using Java 8, Struts 1.2, Jsps, and Chrome
Javascript:

function validateFile(file)
{
filepath = file.value; /*This is null*/
return true;
}


Html:

<input type="file" id="theFile[0]" onChange="validateFile(this)"/>

Answer

I guess it wasn't too much work after all :)

function validateFile(file)
{
    filepath = file.value;
    document.getElementById('result').innerText = filepath;
    return true; 
}
<input type="file" onChange="validateFile(this)"/>
<div id="result">[result will be here]</div>

Comments