473,408 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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

392 Expert 256MB
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
4 5168
AricC
1,892 Expert 1GB
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
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
pronerd
392 Expert 256MB
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
AricC
1,892 Expert 1GB
You could use the OpenFileDialog not sure if you are using ASP or PHP etc...
Nov 16 '06 #5

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

Similar topics

3
by: TekWiz | last post by:
I've got a system that automatically generates a form. I have it set up so that the backend will return to the inital form page with an error object in sessions data (assuming the backend detected...
6
by: ALthePal | last post by:
Hi, I'm not sure if we are able to or even how to loop through the web forms in a VB.NET project during design time. In MSAccess we are able to go through the database -> forms collection and...
5
by: Good Man | last post by:
Hi there I'm adding form fields on the fly with some javascript DOM programming. I basically just clone a hidden <div>, then adjust node properties to make this new <div> have unique values...
4
by: Magnus Blomberg | last post by:
Hello! I have a problem when using a hidden field to send a value to the server. Below you can see my code in simplyfied versions. What I'm trying to do is: 1. The user browses for a picture...
2
by: IkBenHet | last post by:
Hello, I am uploading a file using this form in ASP.NET. I have also added a simpel textfield: <form runat="server" enctype="multipart/form-data"> <input type="file" id="oFile" Name="oFile"...
8
by: Nathan Sokalski | last post by:
I have a System.Web.UI.HtmlControls.HtmlInputFile control that I use to submit files. After the file is successfully submitted, I want the field to be reset so that the user knows the file was...
1
by: Jim S. | last post by:
hi guys and gals, recently u may have seen or noticed that some of your scripts including mine, for picture preview stoped working especially if u are using IE 7, since they changed the content of...
1
by: mark | last post by:
Forgive me if this seems like a stupid question but I need help... I'm trying to do a simple online form that emails me the results from a few fields. Here is the code: <form...
19
by: VUNETdotUS | last post by:
Firefox showed me a security error when I was copying a value of file field object into new DOM created input file field. Can I do something about it? Thanks.
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.