Reputation: 9158
Can I pick the file path from an input tag in javascript? For example:
<input id="file" type="file" onchange="getpath()" />
Upvotes: 0
Views: 3530
Reputation: 51685
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);
};
Upvotes: 1
Reputation: 10874
This can be done in some older browsers, but in correct implementations, the Javascript security model will prevent you from reading the path.
Upvotes: 2