wood burning stoves 2.0*
The moose likes HTML, CSS and JavaScript and the fly likes File Upload Issues Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "File Upload Issues" Watch "File Upload Issues" New topic
Author

File Upload Issues

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1490
    
    1
I am tring to use jQuery.getJSON to do a servlet request for a file upload process. I get the error:

[11/21/08 8:30:35:690 CST] 00000045 SystemOut O org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null

I have enctype="multipart/form-data" in the form tag of requesting JSP.

My requesting code is:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61769
    
  67

I'm not sure how you are expecting this to do a file upload. Where does the multi-part form with the file control come into play?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1490
    
    1
I have no idea other than the requesting JSP has the form with associated file field. I am just trying to find a way to do a file upload using jQuery with a response return and not have to leave requesting JSP.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

For security reasons input fields of type 'file' are very insulated from Javascript by the browser.

If you want to perform a file upload without refreshing the parent page, you might want to consider putting the file upload form in an iframe.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1490
    
    1
Originally posted by Ben Souther:


If you want to perform a file upload without refreshing the parent page, you might want to consider putting the file upload form in an iframe.



Can you give me some direction on how to accomplish this? I have created a JSP called fileuploadbase with my file input tags. I am using this in an iframe in a JSP called fileupload. Where do I put my button to do the submit? What can I do to get a return response of the status?
[ November 21, 2008: Message edited by: Steve Dyke ]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
I do not like how this was coded, but it should give you a basic idea on feel on how it can be done with iframes.

http://www.webtoolkit.info/ajax-file-upload.html

Eric
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61769
    
  67

If you want to do this under script control and using jQuery, you'll need to grab the Form Plugin and submit the multi-part form using its methods. It knows how to automatically use an iframe "under the covers" to perform a file upload. XHR itself cannot do this.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1490
    
    1
How can I call a JS function that is located in my iframe from my parent page?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61769
    
  67

As I recall, there are problems with doing that. But why would you want to? It's a problem waiting to happen. How would the parent know when the iframe has completely loaded and it's safe to access the document?

What is it you are really trying to accomplish? Perhaps a better way can be suggested.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1490
    
    1
My JSP have a form and ability to browse for 5 different files for uploading. When the form is submitted I do not want the page to shell out to another page however, I do want a return message saying succedded or failed.

I have added this function to be called when button is clicked but it does not seem to fire the servlet. It is stopping on the document.ready line of code.


[ November 21, 2008: Message edited by: Steve Dyke ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61769
    
  67

Now I'm really confused. What are you trying to accomplish with the $().ready() call?

That will establish a ready handler to be called when the DOM is loaded. Calling it after-the-fact, like in a form submission handler, won't do anything at all since the event that it would react to has long since occurred.
[ November 21, 2008: Message edited by: Bear Bibeault ]
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1490
    
    1
If the expert is confused think about how I feel just beginning. I could not do any of this with the help of you and others at JavaRanch.

I got my ideas from: http://malsup.com/jquery/form/#code-samples

So what do I need to do to make this work? Or is there some better way?
[ November 21, 2008: Message edited by: Steve Dyke ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61769
    
  67

File upload is actually rather simple -- I'm confused about what you are trying to do.

So:
1) You have a multi-part form with a number of file controls
2) You use the Form Plugin's ajaxSubmit() to submit the form to a servlet, which I assume correctly handles the upload(s)
3) ...
4) Profit!

What is it you are trying to do in step 3 that you were using $().ready() for?
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1490
    
    1
The ready statement was not intentional. It came from the example.
Here is my revised code which does not work because the ajax.Submit call never submits the form.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61769
    
  67

How is subForm invoked? I originally assumed that it was the submit handler for the form, but it can't be as you are declaring the submit handler inside that function.
[ November 21, 2008: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61769
    
  67

And on another note: rather than tacking a query string onto the form action (messy, messy, and rather non-standard even if it works), it'd be better to add these values to the submission by setting them into hidden inputs.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1490
    
    1
I finally got it figured out and working as desired. Thanks for all the help.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61769
    
  67

Welcome!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File Upload Issues