By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,398 Members | 1,372 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,398 IT Pros & Developers. It's quick & easy.

Trick to be able to set the value of a File input form field

Expert 100+
P: 392
Does any one know of any tricks to be able to set the value of a file input field? Setting the value attribute does not work on this type of input field. If there is a way to do it I am assuming it would be with JavaScript.
Nov 15 '06 #1
Share this Question
Share on Google+
4 Replies


Expert 100+
P: 1,892
Not sure why the value attribute isn't working. Here is javascript:

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html>
  4. <head>
  5.  
  6. <script type="text/javascript">
  7.  
  8. function Test()
  9. {
  10.  
  11. var test = document.getElementById("Test").value = "Yo"
  12.  
  13. }
  14.  
  15. </script>
  16.  
  17. </head>
  18. <body onload="Test();">
  19.  
  20. <form>
  21.  
  22. <input id="Test" type="text">
  23.  
  24. </form>
  25.  
  26. </body>
  27. </html>
Nov 15 '06 #2

P: 12
Using the value attribute in a file input is not widely supported... Some browsers do support it, most don't (due to obvious security reasons)

See article below:

Setting the default filename
The HTML 4.01 specification describes the value attribute for a file input field by saying that browsers (user agents) "may use the value of the value attribute as the initial file name". This however is usually not supported by browsers. The usual explanation is "security reasons". And indeed it would be a security risk if files from the user's disk were submitted without the user's content. It might be all too easy to lure some users into submitting some password files! But in fact RFC 1867 duly notifies this problem; in section 8 Security Considerations it says:

It is important that a user agent not send any file that the user has not explicitly asked to be sent. Thus, HTML interpreting agents are expected to confirm any default file names that might be suggested with <INPUT TYPE=file VALUE="yyyy">.
It also mentions (in section 3.4) that the use of value "is probably platform dependent" but then goes on: "It might be useful, however, in sequences of more than one transaction, e.g., to avoid having the user prompted for the same file name over and over again." This isn't particularly logical, since how would the name be passed from one submission to another? (The mechanism for getting the original file name would be quite unreliable for such purposes.) A more useful application could be this: Assume that your form is for reporting a problem with a particular program, say Emacs, and that program uses a configuration file with some specific name, say .emacs, so that you would very much like to get the user's config file for problem analysis. Setting the default name, if supported by the browser, might be an extra convenience to the user.

Thus, they just failed to implement it, for no good reason. This isn't a very important flaw, however. The situations where it would make sense to suggest a default file name are rare.

Netscape's old HTML Tag Reference says, in the description of input type="file", that "VALUE=filename specifies the initial value of the input element", but no actual support to this in Netscape browsers has been reported. Similar considerations apply to the corresponding item in Microsoft's HTML Elements reference. It additionally messes things up by describing the intended meaning wrong: "Sets or retrieves the value of the <INPUT type=file>". The description links to a description of the value attribute which says: "The value, a file name, typed by the user into the control. Unlike other controls, this value is read-only." This probably relates to using the value property in client-side scripting. And in fact, one can read the value in JavaScript (and get the filename entered by the user) but setting it is unsuccessful (without an error message); the same applies to Netscape (but on Opera, even an attempt to read the value seems to confuse the browser). Note that the examples in the above-mentioned documentation do not contain an input type="file" element with a value attribute.

However, support to file input in several versions of Opera handles the value attribute in the following way:

the value is displayed in the box for file name input
that value can be edited by the user (as an alternative to using the Browse menu, which changes the content of that box)
however if the user submits the form so that the initial value has not been changed by the user, there will be a security alert and the user is requested to confirm the submission.
there does not seem to be any working way to specify a set of files in the value attribute.
Such support, however, is absent in Opera 7.54, for some reason.
From: http://www.cs.tut.fi/~jkorpela/forms/file.html#value
Nov 15 '06 #3

Expert 100+
P: 392
Using the value attribute in a file input is not widely supported... Some browsers do support it, most don't (due to obvious security reasons)

See article below:



From: http://www.cs.tut.fi/~jkorpela/forms/file.html#value
Thanks that clears it up. I had not considered the security problems.
Nov 16 '06 #4

Expert 100+
P: 1,892
You could use the OpenFileDialog not sure if you are using ASP or PHP etc...
Nov 16 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.