views:

256

answers:

1

When I call val() on an input with type="file" I only get file name rather than full path. How can I get full path?

+4  A: 

You can't: It's a security feature in all modern browsers.

For IE8, it's off by default, but can be reactivated using a security setting:

When a file is selected by using the input type=file object, the value of the value property depends on the value of the "Include local directory path when uploading files to a server" security setting for the security zone used to display the Web page containing the input object.

The fully qualified filename of the selected file is returned only when this setting is enabled. When the setting is disabled, Internet Explorer 8 replaces the local drive and directory path with the string C:\fakepath\ in order to prevent inappropriate information disclosure.

In all other current mainstream browsers I know of, it is also turned off. The file name is the best you can get.

More detailed info and good links in this question. It refers to getting the value server-side, but the issue is the same in JavaScript before the form's submission.

Pekka
I can't get the path to the file using the browser? This is madness!!
gruszczy
@grusz Well, it depends on your viewpoint: It's none of the web site's business where the uploaded file comes from really. It's just painful when you want to store the original file location as a service for the user, I know.... Flash based upload components like uploadify or swfupload.org *may* have more liberties here.
Pekka
So this means, I can't copy value from one file input to another, because I never have the full path?
gruszczy
@grus you couldn't do that even if you knew the full path. You can't populate a file upload programmatically, also for security reasons. Otherwise, you could go and fetch `C:\Documents and Settings\Username\Top Secret Document.doc` from the visitor's computer without him noticing.
Pekka
Ok, seems I have to redesign now. Thanks a lot.
gruszczy