Skip to content Skip to sidebar Skip to footer

File Path From Input Tags In Javascript

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

Solution 1:

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

Solution 2:

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

Post a Comment for "File Path From Input Tags In Javascript"