tags:

views:

67

answers:

4
<input type="file" />

How can I set the value of this?

+6  A: 

You cannot due to security reasons.

Imagine:

<form name="foo" method="post" enctype="multipart/form-data">
    <input type="file" value="c:/passwords.txt">
</form>
<script>document.foo.submit();</script>

You don't want that websites are able to do this, do you? =)

BalusC
+1  A: 

You can't. And it's a security measure. Imagine if someone writes a JS that set's file input value into some sensitive data file?

Eimantas
+1  A: 

You can't.

The only way to set the value of a file input is by the user to select a file.

This is done for security reasons. Otherwise you would be able to create a Javascript that automatically uploads a specific file from the clients computer.

Guffa
A: 

Not an answer to your question (which others have answered), but if you want to have some edit functionality of an uploaded file field, what you probably want to do is:

  • show the current value of this field by just printing the filename or URL, a clickable link to download it, or if it's an image: just show it, possibly as thumbnail
  • the <input> tag to upload a new file
  • a checkbox that, when checked, deletes the currently uploaded file. note that there's no way to upload an 'empty' file, so you need something like this to clear out the field's value
Wim