Google Answers Logo
View Question
 
Q: Using the <input type=file> tag ( Answered 5 out of 5 stars,   4 Comments )
Question  
Subject: Using the <input type=file> tag
Category: Computers > Programming
Asked by: yochad-ga
List Price: $2.00
Posted: 03 Sep 2002 11:44 PDT
Expires: 03 Oct 2002 11:44 PDT
Question ID: 61326
I am already familiar with using the "<input type=file>" to select a
file from a user's system. However, when the page is refreshed, or
when it is necessary to go to a next page where that same information
is needed, it disapears.
How can i set a default value to the <input type=file> tag? 
I already tried <input type=file value="C:\howdy.txt"> and it didn't
work.
Answer  
Subject: Re: Using the <input type=file> tag
Answered By: joseleon-ga on 03 Sep 2002 12:49 PDT
Rated:5 out of 5 stars
 
Hello:
 
I'm sorry but you can't do that, why? security reasons, read below:

Input Type=File
http://www.blooberry.com/indexdot/html/tagpages/i/inputfile.htm

"Internet Explorer, Netscape and Opera do not use the VALUE attribute
as the default contents of the input area. Any default value set via
HTML is not usable via scripting and the DOM as well (hence it is not
listed as 'supported' in any of the browsers.) If a user enters text
in the field however, that value is then reachable via the DOM as it
normally would be for a normal INPUT field (via the .value property.)
The reason for this behavior would presumably be to ensure the
security/safety of users against malicious authors."

And here you can read a very interesting discussion about this:

File input (or "upload") in HTML forms - Setting the default filename
http://www.cs.tut.fi/~jkorpela/forms/file.html#value

"The HTML 4.0 specification says that for a file input field, 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">."

I recommend you to read the entire page, it's very interesting and it
points you to a link where a user did that on Netscape:

File input (or "upload") in HTML forms - The appearance of the Browse
button and the filename box
http://www.cs.tut.fi/~jkorpela/forms/file.html#present

How to assign a value to "Browse..." button? 
http://groups.google.com/groups?oi=djq&as_umsgid=%3C38119FEE.DFF60EBA%40sector27.de%3E

I include some code to show you how you can read that field but now
write to it even using JavaScript:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script language="JavaScript">
<!--
function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document;
if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++)
x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++)
x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n);
return x;
}

function MM_showProp(objName,x,theProp,theValue) { //v3.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)) eval("
alert(obj."+theProp+");");
}

function MM_setTextOfTextfield(objName,x,newText) { //v3.0
  var obj = MM_findObj(objName); if (obj) obj.value = newText;
}
//-->
</script>
</head>

<body bgcolor="#FFFFFF" text="#000000">
<form name="form1" enctype="multipart/form-data" method="post"
action="">
  <input type="file" name="file">
  <input type="button" name="Button" value="Show"
onClick="MM_showProp('file','','value');">
  <input type="button" name="Button" value="Set"
onClick="MM_setTextOfTextfield('file','','yourtext');">
</form>
</body>
</html>

I hope this is what you were looking for and don't hesitate to ask for
any clarification.

Regards.

Request for Answer Clarification by yochad-ga on 03 Sep 2002 15:05 PDT
since you can't set a default value for the <input type=file> tag, can
you set a default value for a text box and then use the value returned
there to do a file upload?

Something like this?

<input type=text name=txtbox value="C:\himom.txt">
.
Then in my perl cgi script query the value of the txtbox parameter and
use
that value (which would be a file name on the user's computer) as the
name of the file to upload.

If i still can't do that, I am getting errors in the javascript
example that you sent me, could you check it over to make sure it's
correct?

Thanks,
    Chad

Request for Answer Clarification by yochad-ga on 03 Sep 2002 15:45 PDT
maybe i should describe what it is that i am doing to help you help
me.

I wrote a cgi script that (when called with no parameters) shows a
menu. The menu has your standard radio buttons, and checkboxes and it
also has the <input type=file...> tag. When the user fills in all of
the necessary feilds and clicks on the submit button, the script does
a query on a data base and combines that information wiwth
prespecified information stored on the users computer in the file that
they selected and shows all of that info in an intelligable format to
the user (we'll call that the info page).

However the specifications have been modified now so that the menu
will again  appear at the top of the information page so that the user
can change the selections that were made. When the info page comes up,
all of the selections on the menu are defaulted to the most recent
selections that the user has made, so that if the user only wants to
change 1 of the many feilds and resubmit the query, it can be done
without having to worry about setting up the menu all over again.
However, because there isn't a way to default this -> <input
type=file> , i need a way to be able to store the last filename that
the user entered and then re-upload that file if the user decides not
to change that field in the menu. How do i do that?

Clarification of Answer by joseleon-ga on 03 Sep 2002 23:37 PDT
Hello:

>Then in my perl cgi script query the value of the txtbox parameter 
>and use that value (which would be a file name on the user's
computer) as
>the name of the file to upload.
A file is uploaded by the browser to your server when you specify the
enctype="multipart/form-data" in your form, is the browser who uploads
the file, not you, so what you say it's not possible, check here:

The enctype attribute
http://www.cs.tut.fi/~jkorpela/forms/file.html#enctype

"It seems that the HTML 4.0 specification contains no explicit
requirement that enctype="multipart/form-data" be used if the form
contains a file input field (although it explicitly recommends that).
But e.g. IE 4 and Netscape 4 handle form submissions incorrectly if
the enctype is defaulted in such a case: they send the name of the
file instead of its content!"

>i need a way to be able to store the last filename that
>the user entered and then re-upload that file if the user decides not
>to change that field in the menu. How do i do that? 
If the user leaves the input file in blank, it means he/she doesn't
want to make any change, you can place a label "Leave in blank to
don't make changes" just at the side of the input file. I know it's
not very polite, but be aware this is a browser limitation and you
have to find a workaround bearing in mind you cannot upload a file to
the server if the user has not selected it.

Regards.

Clarification of Answer by joseleon-ga on 04 Sep 2002 01:18 PDT
Hello:
 About the problems you say with the code I posted, please, try to
readjust the code because when I posted to google, it has break some
lines and this could be your problem. If not, please, post which
browser/OS you are using.

Regards.
yochad-ga rated this answer:5 out of 5 stars
Thank you so much for your dillagence to helping me understand and
then provide a work around for my file (re-)uploading problem. You
were very informative and the links you provided were great! I hope
that you answer next time i have a problem!

Comments  
Subject: Re: Using the <input type=file> tag
From: darthcow-ga on 03 Sep 2002 15:15 PDT
 
You still can't do that.

About that javascirpt example - it looks like an example of JS from a
WYSIWYG editor :(.

To read an attribute from a file box, I'd think that a simple script
would work. Just assign the form a name (here it's "formname") and the
upload field a name (here it's "uploadname").

You can access that value in javascript like this (I think): 

<script>
function getuploadedfilename() {
  return document.formname.uploadname.value;
}
</script>

Just call the function getuploadedfilename() when you need it :).
Subject: You CAN'T
From: darthcow-ga on 03 Sep 2002 22:10 PDT
 
It's a simple security feature. If that were possible, sites would be
able to upload personal data with a user not knowing. Just make a huge
form/page and hide the preset file upload form somewhere :/.

I'd recommend simply saving the uploaded file to the site so that if
there are no changes there is no need to upload it again - but if
there are changes, there is no way to get around this.
Subject: Re: Using the <input type=file> tag
From: joseleon-ga on 03 Sep 2002 23:29 PDT
 
>About that javascirpt example - it looks like an example of JS from a
>WYSIWYG editor :(.
It's created with Dreamweaver, is there any problem to post code
generated with it? Including Dreamweaver's generic JavaScript routines
to find objects and change/show object properties make the code more
safer and cross-browser. Also, I would like you to test the code you
posted on any browser including Opera and Netscape 4, I bet it doesn't
work.

Regards.
Subject: Re: Using the <input type=file> tag
From: darthcow-ga on 04 Sep 2002 21:00 PDT
 
Dreamweaver code does work, but it certainly isn't the most effecient
way to do things - there are easier ways to check for existence of an
object than all the messing around in arrays and indecipherable junk.
Personally, I like to understand what my code is doing. The truth is,
I don't know if it works in Opera and Netscape 4, though you still get
the far majority of users with just IE compatability.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy