Can I pick the file path from an input tag in javascript? For example:
<input id="file" type="file" onchange="getpath()" />
Can I pick the file path from an input tag in javascript? For example:
<input id="file" type="file" onchange="getpath()" />
This can be done in some older browsers, but in correct implementations, the Javascript security model will prevent you from reading the path.
You can’t get at the full path to the file (which would reveal information about the structure of files on the visitor’s computer). Browsers instead generate a fake path that is exposed as the input’s value property. It looks like this (for a file named "file.ext"):
C:\fakepath\file.ext
You could get just the filename by splitting up the fake path like this:
input.onchange = function(){
var pathComponents = this.value.split('\\'),
fileName = pathComponents[pathComponents.length - 1];
alert(fileName);
};