• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

validating file uploads

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have created a form that allows a user to upload a file, and I expected to see that the corresponding FormFile within my form bean would be null if the user neglected to choose a file to upload and simply clicked submit. Of course, I was wrong and it looks like I still receive a valid FormFile object regardless. Which is fine!

My problem is that I don't see an elegant way to determine whether or not the user actually uploaded a file other than looking at the filename or size attributed of the FormFile object. Additionally, the examples I have found for this procedure don't seem to address this matter at all, and assume that something has been uploaded.

Have I missed something very basic that explains why there isn't an obvious way to do what I'm trying to do or have I just not found the solution yet?

Any help would be appreciated!
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my code I check the fileSize property...here is an example:


- Brent
 
Kyle Cartmell
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In my code I check the fileSize property...



I was afraid I'd have to resort to that!

At first glance (I'll tinker on my own, of course...) it would appear that this solution prevents a user from uploading an empty file.

Can anyone supply an alternative to this approach, or failing that provide me with some feedback on the idea of testing for a blank filename and a file size of zero to identify cases where no file was uploaded? (Can you have a blank filename but a size > 0? I suspect that there's a way...)

On a side note, any thoughts on why null isn't passed instead of a somewhat unpopulated FormFile object?

Thanks for the help!
[ March 21, 2006: Message edited by: Kyle Cartmell ]
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a reason that a user would want to upload an empty file?

- Brent
 
Kyle Cartmell
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is there a reason that a user would want to upload an empty file?



Is there a reason why a user shouldn't be able to upload a blank file?

Okay, so it doesn't make sense in my application. However, the logic is still useful to me because I need to differentiate between no file having been uploaded and a blank file being uploaded so that my error messages make sense to the user and I don't have to field a support ticket when this happens. ("no file was uploaded!" vs. "uploaded file was empty!")
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would show a message saying "The file you selected is invalid or empty" and move on to the next requirement.

- Brent
 
Kyle Cartmell
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Huh. Point well taken.

Thanks for all the help, I appreciate it.
 
Kyle Cartmell
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do so enjoy the occasional beating of an ungulate corpse!

I found a very interesting comment in the Struts html taglib reference:

In order to correctly recognize uploaded files, the ActionForm bean associated with this form must include a statement setting the corresponding org.apache.struts.upload.FormFile property to null in the reset() method.



This suggests to me that setting my FormFile property to null in the reset method should produce the behavior I expected in the first place. Which, of course, it does not.

Any thoughts on this?
 
reply
    Bookmark Topic Watch Topic
  • New Topic