jblue
jblue

Reputation: 4450

Reading uploaded file path with jquery

I have an upload field and I want to read user input with jquery

My html is this

<input type="hidden" id="MAX_FILE_SIZE" value="1999500" name="MAX_FILE_SIZE">
<input type="file" id="upl-0" name="upl[]">
<input type="file" id="upl-1" name="upl[]">
<input type="file" id="upl-2" name="upl[]">

In my jQuery, I do this

var paths = '';
$('input[type=file]').each(function(){
    paths = paths + $(this).val();
});    
alert(paths);

but I'm getting only the file name, not the full path. When I select C:/folder/file.doc, I get only file.doc. I'm trying to get this path to populate the field again on a failed form attempt without the user having to reselect the files, so I need the full path for this to work. How can I get it?

Upvotes: 1

Views: 3379

Answers (1)

Marcel Korpel
Marcel Korpel

Reputation: 21763

Generally, you can't. It would be a huge security issue if you can capture the full path name and (re)populate the value of the upload (file) field with that (or another) file name using JavaScript.

A malicious site can do this without the user knowing it does so and upload (in fact, steal) files from someone's computer.

There may be workarounds for this in several browsers (e.g., using some ActiveXObject), but those are not widely supported (and newer browsers tend to prevent actions like these).

Upvotes: 3

Related Questions