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?
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 ]
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!")
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.