Sunday, May 21, 2006

Struts and file upload

I'm trying to use a form that has a array of FormFile as a field of the form.

I can use to display the FormFiles with no problem, but when I submit the form, the array only has null values.

To be completely specific, I made a bean where one of the fields is a FormFile.
And my form contains an array of this bean.

When I submit the form, the action gets an array of my bean, but the FormFile attribute is null.

If I create a FormFile field directly in the form and submit it, it works as expected, giving me the uploaded file information.

Is it not possible in struts to upload a variable number of files?

I'm sure it's possible, but I have a feeling it'd be easier to just declare five FormFile fields and deal with that.

update: 2006-05-22:
I made two now obvious mistakes:

1) Need to set indexed="true" for the fields generated inside my iterate loop. This way, instead of generating (in my case) five < input name="uploadFile" type="file"> fields, I can generate
< input name="images[0].uploadFile" type="file">
< input name="images[1].uploadFile" type="file">
< input name="images[2].uploadFile" type="file">
< input name="images[3].uploadFile" type="file">
< input name="images[4].uploadFile" type="file">
where each field has a distinct name (except that there is no space between "<" and "input" normally; I added it so you could see my code).

2) My second mistake was setting the id attribute in the iterate tag to a string different from the name of my file array. My file array is called images, and I set the iterate id to "imageArray." This would generate five fields called imageArray[0].uploadFile, imageArray[1].uploadFile, etc. Since imageArray doesn't exist in my form bean, upon submission of the form the file data gets lost since there is no parameter to save imageArray data to.

Both of these mistakes are really obvious to me now, but I shouldn't have made them at all considering that I encountered the same issues half a year ago. Bleh.

update 2006-06-26: I did have to do a little fiddling to get the validation errors displayed properly. The html:errors tag sadly does not support iteration directly, so you have to use jsp expressions to get the index appropriately.

Labels: , ,

0 Comments:

Post a Comment

<< Home