3

I want to upload multiple files using single Struts 2 file tag.

Like in Gmail we attach multiple files using CTRL key to select multiple files.

I know how to upload multiple files but I want to use single file tag.

Roman C
  • 49,761
  • 33
  • 66
  • 176

1 Answers1

1

I upload multiple files in a little gallery application. If your action is already set to receive multiple files, then it is as simple as (Warning this works in just about every major browser except IE, so you will need a jQuery or flash based solution for this):

<s:form namespace="/gallery" action="image-upload" method="POST" enctype="multipart/form-data">
    <s:file name="image" multiple="multiple"/>
    <s:submit/>
</s:form>

You've probably already looked at the parameter which sets the maximum file size (for a single file), by default this is 2MB if I remember correctly and there is a different value for the maximum total file size (that is the size of all files summed together). If a user is able to upload a good collection of images increasing this later value is very useful, to set this value to roughly 20 MB (in struts.xml):

<constant name="struts.multipart.maxSize" value="20000000" /> 

Edit: For the interested, the html rendered from the above is...

<form id="image-upload" name="image-upload" action="/PhotoGallery/gallery/image-upload.action" method="POST" enctype="multipart/form-data">
    <input type="file" name="image" value="" id="image-upload_image" multiple="multiple"/>
    <input type="submit" id="image-upload_0" value="Submit"/>
</form>

UPDATE 2014 Feb (almost 2 years later): The multiple attribute is now supported by Internet Explorer 10, Firefox, Opera, Chrome, and Safari.

It was not supported by Internet Explorer 9 and earlier versions.

Quaternion
  • 10,380
  • 6
  • 51
  • 102
  • i never saw `multiple` as file parameter.Can you link me to doc where it has mentioned as its really an interesting thing – Umesh Awasthi Mar 22 '12 at 07:41
  • never saw multiple attribute for a file tag – Qazi Sameer Ahmed Mar 22 '12 at 08:48
  • +1. @UmeshAwasthi: [as described here](http://stackoverflow.com/questions/16393581/need-to-upload-multiple-files-at-once/17212916#17212916), Struts `` element allows Dynamic Attributes, this means that you can put every kind of existing (or not) attribute and it will be rendered in JSP with no check by Struts. – Andrea Ligios Jun 20 '13 at 11:54
  • @AndreaLigios: that is really interesting..Thanks for the link – Umesh Awasthi Jun 20 '13 at 12:01