Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Uploading Image file from client to the server

 
carina caoor
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am tryign to upload an image file from client machine to the server for that i am using Commons FileUpload from Apache. the html i have designed is



and the servlet Fileupload takes the multipart content from the request and saves the file successfully on the server.

But here as i am clicking the button upload the current page where the Image gets uploaded is replaced by a blank page which is normally the feature of a submit button. i want to stay in the same page and upload the image can anybody suggest me how do i do this.

I tried using Ajax to call the servlet.. being able to stay on same page using Ajax the file multipart content is not passed to servlet and file do not get uploaded.

 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are not setting anything to be submitted to the server and not all browsers support uploading files via Ajax. Use the jQuery plugin to do it.

Eric
 
carina caoor
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even when i submit the form directly without using AJAX, i was not setting anything to be submitted to the servlet .. it was directly getting the data as described in the link http://commons.apache.org/fileupload/using.html .... instead of directly submitting the form i was trying to do it from Ajax ... The servlet was getting called but the file along with the content was not getting streamed.

I also used JQuery to call the servelt as $.post('FileUpload',{},function(data){});

Still the servlet gets called up but teh file is not getting saved on the server.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
 
carina caoor
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THe above link describes the method on Mozilla.. i am tryign to execute the given examples in IE... as my application has to run on IE ... and it gives me errors saying FormData is undefined... i googled and found that FormData element is supported by Mozilla not by IE.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And as I stated before, YOU CAN NOT USE THE XMLHTTPREQUEST OBJECT TO UPLOAD A FILE WITH ALL BROWSERS. Look on the web, there are solutions already made that will upload a file asynchronously using a hidden iframe.

Eric
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"rt ssum", please check your private messages for an important administrative matter. Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic