Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I set the file name for a file upload? 1

Status
Not open for further replies.

atomix

Programmer
Aug 14, 2002
16
0
0
US
I have a form that uploads a file and that is working fine. My user complains that every time they want to upload, they have to browse through their ugly directory structure to find the same file time and again. So I thought I'd be nice and capture the file name and write it to a cookie on their system when they hit the submit button, then when they return to the page later, I'd retrieve the info in the cookie and stuff it in the box where the file name goes. The cookie code is working - I have put several alert() stmts so I know I am saving and retrieving the file name OK. The problem is getting the file name into the filename box - how do I do that, or is it even possible?

Here's the code for the form:
Code:
<form action="roster_upload01a.php" method="POST" enctype="multipart/form-data" name="form1" id="form1" onSubmit="validateForm(this.form);">

<input type="hidden" name="MAX_FILE_SIZE" value="30000" />

        Send this file:
        <input id="userfile" name="userfile" type="file" />
        <input type="submit" value="Send File" />
</form>

In js, i have tried:
document.getElementById("userfile").value = savedFileName;
and
document.form1.filename.value = savedFileName;
with no luck.

Ideas/suggestions? Is this possible? Thanks in advance.
 
Where's the Javascript that fills in the information? You have to show it in relation to the code you have. You don't need to show us ALL the HTML, just where the JS is compared to the form you posted above.

Lee
 
Sorry...
The code above is in a script that resides between the HEAD tags, and is called when the page is loaded. Something like this:
Code:
<head>
<title>Bland Title</title>
<script language="javascript">
function loadForm(thisForm) {
	var this_filename = getCookie("TheCoolJavaScriptPage");
	//alert("The file name is " + this_filename);
	document.getElementById("userfile").value = this_filename;
}
</script>

</head>

<body onLoad="loadForm(this.form);">
...
I think it has something to do with the fact that this field is of the type "file" and not "text". I can add a plain text field on the form like this:

Code:
<input type="text" name="play_field" id="play_field">
then add this line to the loadForm function above:
Code:
document.getElementById("play_field").value = this_filename;
and "play_field" will be set to the value stored in the cookie, but "userfile" will not be set. It's got me stumped. But then again, that's why I'm here :)
 
Why are you using this.form in your function call onload? That doesn't refer to anything, and could create an error. As well, since you're not referring to the argument in the function declaration thisform (without the period), why do you have it in there?

Lee
 
This was a hang-over from some previous code - "thisform" was referred to but I have taken it out to cut down on the amount of code to paste here. I originally referred to the field through the passed form object, but in trying to figure out why it wasn't working, I went the more "direct" route with by using document.getElementById... and document.form1.userfile... routes, all to no avail.

Changing it to:
Code:
<script language="javascript">
function loadForm() {
    var this_filename = getCookie("TheCoolJavaScriptPage");
    //alert("The file name is " + this_filename);
    document.getElementById("userfile").value = this_filename;
}
</script>

</head>

<body onLoad="loadForm();">
doesn't change the results - the field "userfile" doesn't get set where the field "sample" does.
 
Small typo in the above: The last code snippet should be:
Code:
<script language="javascript">
function loadForm() {
    var this_filename = getCookie("TheCoolJavaScriptPage");
    //alert("The file name is " + this_filename);
    document.getElementById("userfile").value = this_filename;
    document.getElementById("play_field").value = this_filename;
}
</script>

</head>

<body onLoad="loadForm();">
...
(notice the addition of the line to set play_field)

and the very last line should have read...

doesn't change the results - the field "userfile" doesn't get set where the field "play_field" does.

Sorry for any confusion...
 
Thanks, tsuji

I thought this might be the case, but I couldn't find it documented anywhere. My user won't be happy, but so goes things. Thanks again!
 
Thanks for the vote! But your users should be happy rather than not. The feature means this. If the page in disguise allows the input to upload a file at user's choice and consent. In the onsubmit handler, the page is coded such that it will automatically alter the file type input's value and make the page upload another file which might contains secret info instead. Would the user be happy? That is bascially what it is meant by the security-feature.
 
I understand the concern and the security risk of letting a script change the filename before it was actually submitted. But this environment will never have public access, and the max machines on the net would be about 5, maybe 7 or so, so the environment is controllable enough from the user's point of view. So he won't be happy, but at least he'll have his answer. Again thanks to all that helped.
 
atomix,
You cannot automatically insert the file name for the upload from a WEB page but there is an alternative.

If your clients are using IE you could code your page as an HTA file. HTA (HTML for Applications) run client-side with client privledges so you CAN write code to do an automatic upload of a specific file.

The drawback is that the client will need to launch the .HTA file on their PC for it to operate. You can set a web page to deliver the .HTA page to the client but then it will pop up an ActiveX warning and ask if they want to save or open the file.
If you just want to automate the upload of a file for the clients you can make it all happen through the one icon.



Stamp out, eliminate and abolish redundancy!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top