Reputation: 31
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.
Upvotes: 3
Views: 8144
Reputation: 10458
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.
Upvotes: 1