• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

upload an avatar or other file

 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want a webpage/jsp to let the user specify a local file to upload to my web application/servlet/bean
I can't grok what I need to do. Code snippet:



This lets the client browser select the file. But all I get is the file name, not the contents.
Clearly I'm doing something wrong.

I don't know if my error is in the JSP/html or my servlet or bean or ...

Thanks
Pat
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you need to add enctype="multipart/form-data" to your form tag.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See the JSP FAQ for the entry on file uploading.

[Edit:]
Here, in fact.
 
Pat Farrell
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:See the JSP FAQ for the entry on file uploading.


I did that, and added the type as suggested upthread, so the code in the JSP looks like:



Loaded the Jakarta Commons FileUpload

However, when the POST is received, there are no arguments in the query string. and


is always false.

So I'm still missing something, probably fundamental.
thanks
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pat Farrell wrote:However, when the POST is received, there are no arguments in the query string.


There won't be. All data is in the request body.



arg.getRequest()?

I'd use a tool like HttpFox at this point to see what's being sent in the request.
 
Pat Farrell
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The ServletFileUpload.isMultipartContent(); function wants a HttpRequest.

When one is using FrontMan, you have a CommandContext, and to get the request you call
arg.getRequest()

I'm not seeing anything in the body either. I'll look again.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, it was the "arg" that threw me -- I call it "commandContext".

I'm not seeing anything in the body either. I'll look again.


An empty request body would indicate a problem on the browser side of things.
 
Pat Farrell
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:An empty request body would indicate a problem on the browser side of things.


Agree, I'm just using Firefox 3.5
Perhaps I should try Chrome or something else, altho in Ubuntu, Firefox and Chrome are usually enough.
 
Marshal
Posts: 28175
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pat Farrell wrote:

Bear Bibeault wrote:An empty request body would indicate a problem on the browser side of things.


Agree, I'm just using Firefox 3.5
Perhaps I should try Chrome or something else, altho in Ubuntu, Firefox and Chrome are usually enough.


I think you'll find that Firefox (any version you care to name) will upload files perfectly well. Blaming the browser isn't going to be a productive strategy for fixing your problem.
 
Pat Farrell
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Found the problem. Its not that FireFox is bad.

I did not understand than when you use the flag enctype="multipart/form-data" in the form, you no longer see any of the field values. They are all hidden in the special "multipart/form-data". So my testing for query parameters or other arguments failed.

Once I realized this, I could use the Apache test code:


and then branch to completely separate code to pull apart the "multi-part arguments.

I'm not happy with the cleanliness of the code, as I can't rely upon any ordering of the multi-part arguments, so I don't know if I have the name I want before I get the file name. But, the basics are working, and I can clean it up later. :-)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic