0

I have multiple browse buttons on my page. I want to save the path of the files selected by the user and not upload the files themselves. Security is not an issue since the page is on on intranet.

Is it possible to get the file path (file name included) so that I can store this value in the database?

Raj
  • 22,346
  • 14
  • 99
  • 142
Pre
  • 113
  • 2
  • 3
  • 12

2 Answers2

1

Nope, you can not get it reliably.

For example, some browsers will return c:\fakepath\file.jpg.

alex
  • 479,566
  • 201
  • 878
  • 984
  • I have replaced the type='file' (browse button and text) with a regular text type= 'text' , so that the user can enter the path and I can save the value as in a normal text. This is a very bad design, since the user cannot 'browse' to the desired file. Hope there was a better work around – Pre Jun 09 '11 at 09:41
-1

Assuming your has an id of upload this should hopefully do the trick:

var fullPath = document.getElementById('upload').value;
if (fullPath) {
        var startIndex = (fullPath.indexOf('\\') >= 0 ? fullPath.lastIndexOf('\\') : fullPath.lastIndexOf('/'));
        var filename = fullPath.substring(startIndex);
        if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) {
                filename = filename.substring(1);
        }
        alert(filename);
}

From How to get the file name from a full path using JavaScript?

Community
  • 1
  • 1
Michael Robinson
  • 29,278
  • 12
  • 104
  • 130