 | 
November 15th, 2006, 05:40 PM
| | Expert | | Join Date: Nov 2006
Posts: 375
| | Trick to be able to set the value of a File input form field
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.
| 
November 15th, 2006, 07:37 PM
| | Expert | | Join Date: Oct 2006 Location: NC
Posts: 1,723
| |
Not sure why the value attribute isn't working. Here is javascript : - <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html>
-
<head>
-
-
<script type="text/javascript">
-
-
function Test()
-
{
-
-
var test = document.getElementById("Test").value = "Yo"
-
-
}
-
-
</script>
-
-
</head>
-
<body onload="Test();">
-
-
<form>
-
-
<input id="Test" type="text">
-
-
</form>
-
-
</body>
-
</html>
| 
November 15th, 2006, 08:52 PM
| | Newbie | | Join Date: Nov 2006
Posts: 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: Quote:
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
| 
November 16th, 2006, 01:49 PM
| | Expert | | Join Date: Nov 2006
Posts: 375
| | Quote: |
Originally Posted by soeter04 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.
| 
November 16th, 2006, 03:56 PM
| | Expert | | Join Date: Oct 2006 Location: NC
Posts: 1,723
| |
You could use the OpenFileDialog not sure if you are using ASP or PHP etc...
|  | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 205,248 network members.
|