This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Dynamically created {input type=file} Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Dynamically created {input type=file}" Watch "Dynamically created {input type=file}" New topic
Author

Dynamically created {input type=file}

Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
Hi friends,
I would appreciate your help here. Thanks, Jopseph.

I m submitting a form that contains an

<span id=gAttachBI><input type=file name="attach"></span>

field. since i have to stay in the same page (page A == an html page) that submits the form, i submit it to a page (page B == a servlet page) that lives in an iframe in page A.

now since i want to let the user upload a file over and over, while i finish processing the sent data on page B, I do:
out.print("<script>window.parent.wm_handleAttach('" + attached_file_name + "');</script>");

now on page a there is a javascript function called wm_handleAttach(filename) that does:

document.all("gAttachBI").innerHTML='<input type=file name="attach">';

Why do I do that? because i stay in the same page after submitting, and i want to clean the file field after i submit!!!

you know i cannot set the value attribute of an <input type=file> thru javascript, since this violates the security of the browser.

so my way to "clean" the field is to re-create it.

this works fine, both in IE and Firefox.

Problem is when I submit the uploaded file thru the dynamically created input field (e.g. in the second time i submit a file), it works fine in IE, but not in FF...

in FF every thing seems to work fine: the browse window opens and it takes it a while to send a large file, but my codein the servlet (page B) does not read it fine.

I am using Jakarta commons upload jar to readthe uploaded file... I have discovered that when I send it thru FF, the condition:



is not true, so it does not continue into the "if" to process the file data. but the same servlet process correctly the file sent from IE.

the problem seems to be that a dynamically created <input type=file> in FF sends other things than a static <input type=file> in FF.

everythings works fine in FF if i do not re-create the <input type=file> after each time I submit. (i.e. i give up trying to clean the <input type=file> field after each time i submit)

Please help if you have any idea......

[ May 09, 2005: Message edited by: Joseph Sweet ]
[ May 09, 2005: Message edited by: Joseph Sweet ]

We must know, we will know. -- David Hilbert
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
document.all is IE only

you should use

document.getElementById('theID')

not sure, but I think
document.formName.elementName.reset();
sets it to blank....


if I were to add an element, I would do it this way



Eric
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
thanks for you response.

1. why does document.all() work in FF?

2. the reset() did nothing in neither IE nor FF.

3. the appendChild(newElem) seems to add the element, but how do i remove the previous one? i.e. how do i remove:

document.formName.elementName

--------------------------------------------------------------------

I have tried to put the following code in wm_handleAttach(filename):





but then when i try to use document.msgform.attach.value (while i submit form the dynamically created file field), IE javascript debugger gives me the error: document.msgform.attach.value is null or not an object.

it seems like dynamically created file fields are not just like static ones?

any ideas?
[ May 09, 2005: Message edited by: Joseph Sweet ]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
I ran the code and for some reason it is not adding the name to the attributes, I am puzzled why it is nt doing it...I will try to figure this out.

Eric
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
ahhhhhhhhhhhhhhhhHHHHHHHHHHHHHHHH

name should be id!



Eric
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
it looks great, but as it is always with javascript, looking great does not mean working.

it does work for me on IE (but not on FF) when i simply do:



[ May 10, 2005: Message edited by: Joseph Sweet ]
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
Eric I have found the following workaround useful:

http://www.siteexperts.com/forums/viewConverse.asp?d_id=15967&start=1
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamically created {input type=file}
 
Similar Threads
Dynamically added form fields cannot access from request access
clearing file input
Session in JSP
JSP session tracking problem
Retain File Selected in a File Upload Component