my dog learned polymorphism*
The moose likes Struts and the fly likes File upload problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "File upload problem" Watch "File upload problem" New topic
Author

File upload problem

Rajeev Ja
Ranch Hand

Joined: May 06, 2007
Posts: 38
I am using struts and have an upload.jsp and its associated UploadForm.java and UploadAction.java. The jsp's form has an enctype=multipart/form-data and contains an html:file field to upload a file as well as a bunch of text fields for metadata associated with the file. If an error occurs during validation, the struts controller servlet forwards to the same jsp with error messages. The problem is that the filepath the user specified is no longer present. Is there a way to restore this value so the user doesn't have to complete this field again (the other text fields remain filled in).
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Originally posted by Rajeev Ja:
Is there a way to restore this value so the user doesn't have to complete this field again?

No, there isn't.

None of the major browsers will allow a default for an upload file field to be set from the server-side. It's a security issue. Think about it: If the browser allowed a default value in this field, what would prevent a malicious website from defaulting the most likely path to your outlook file with all your names and addresses, and using JavaScript to submit the form before you even knew what was happening?

One possible work-around would be to use JavaScript to validate the other fields before the form is submitted.
[ December 18, 2007: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
Rajeev Ja
Ranch Hand

Joined: May 06, 2007
Posts: 38
Instead of validation If i have other button on click of that it will go the action class and all other field values are restored except the browse button file path how to resolve this
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The fact is that once a form is submitted, there is no way to redisplay the <html:file> field with its original contents because of the security issue that I mentioned in my last post. Whatever solution you devise must take into account this fact.

One possible solution would be to make an AJAX call to the server without submitting the form.
Rajeev Ja
Ranch Hand

Joined: May 06, 2007
Posts: 38
No I do not want to use Ajax can any one tell a solution
 
 
subject: File upload problem