Omega
Omega

Reputation: 9158

file path from input tags in javascript

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

Answers (2)

s4y
s4y

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

Jonas H&#248;gh
Jonas H&#248;gh

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

Related Questions