0

Can I pick the file path from an input tag in javascript? For example:

<input id="file" type="file" onchange="getpath()" /> 
1
  • 1
    If by "pick" you mean set it, then absolutely not. If you mean "read" then all that's available is the file name, not the path; stackoverflow.com/questions/2189615/… Commented Dec 19, 2011 at 15:10

2 Answers 2

2

This can be done in some older browsers, but in correct implementations, the Javascript security model will prevent you from reading the path.

Sign up to request clarification or add additional context in comments.

3 Comments

other methods? via flash or other?
@Omega - Using a 3rd-party plugin (Flash or Java) to make a richer client / app can be used to grab the client-side file path, but in all cases, it is a security risk to the client and should require additional privilege escalation. A more important question is why do you need the client-side path?
@Omega - You certainly can do it with a signed Java applet. I hope it is not possible with Flash. If it is, I wouldn't count on it to keep working in future versions.
1

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);
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.