Javascript - Form - upload file error  
Author Message
cormacodonnell





PostPosted: 2004-12-8 1:50:00 Top

java-programmer, Javascript - Form - upload file error I am using a combination of jsp javaScript and html to build a form.
the form cannot have a default button, so what i have done is created
an input type as "file" and made it hidden. i have then called it
using a bit of java script ( uploadObj.click(); )linked to my image,
used to represent the button.


Problem occurs when you go to submt the form. IN IE, running on xp
home, with service pack 2 installed, it empties the textfield. THe
file must be put through as type file, so filling a textfield with the
text is not allowed.

I have included a reduced version of the problem in the code below ( i
have made the file upload visible for test purposes)




<html><head> Form Test </head><body>

<SCRIPT LANGUAGE = "JavaScript">
function BrowseForFile() {
var browseObj = document.getElementById("thumbNail");
browseObj.click();
}
</SCRIPT> </body>

<form Action = "here.html" name="myform" method="post"
enctype="multipart/form-data">

<a onClick="BrowseForFile();">Browse</a>
<input type="file" name="thumbNail" >
<input type="submit" name="sub" value="send now" >
</form>
</html>



Notice how using the default browse button and pressing "send now"
posts the form off to the location requested.

Now try pressing the browse text to the left, upon selecting the file
and pressing the "send now" button it simply empties the text field,
WHY???


Thank you in advance


Cormac O'Donnell
 
Dag Sunde





PostPosted: 2004-12-8 2:52:00 Top

java-programmer >> Javascript - Form - upload file error "Cormac O'Donnell" <email***@***.com> wrote in message
news:email***@***.com...
> I am using a combination of jsp javaScript and html to build a form.
> the form cannot have a default button, so what i have done is created
> an input type as "file" and made it hidden. i have then called it
> using a bit of java script ( uploadObj.click(); )linked to my image,
> used to represent the button.
>
>
> Problem occurs when you go to submt the form. IN IE, running on xp
> home, with service pack 2 installed, it empties the textfield. THe
> file must be put through as type file, so filling a textfield with the
> text is not allowed.
>
> I have included a reduced version of the problem in the code below ( i
> have made the file upload visible for test purposes)
>
> <html><head> Form Test </head><body>
>
> <SCRIPT LANGUAGE = "JavaScript">
> function BrowseForFile() {
> var browseObj = document.getElementById("thumbNail");
> browseObj.click();
> }
> </SCRIPT> </body>
>
> <form Action = "here.html" name="myform" method="post"
> enctype="multipart/form-data">
>
> <a onClick="BrowseForFile();">Browse</a>
> <input type="file" name="thumbNail" >
> <input type="submit" name="sub" value="send now" >
> </form>
> </html>
>

Don't know if this solves your problem, but...

> var browseObj = document.getElementById("thumbNail");
...
> <input type="file" name="thumbNail" >

using .getElementById() you must have an element with that
particular id-attribute. "id" and "name" are not interchangeable
when it comes to getElementById(). I would change it to:

<input type="file" id="thumbNail" name="thumbNail">

PS!
The question is probably better answered in:
comp.lang.javascript

--
Dag.