Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Multiple File Select &amp; Upload Using Spring Framework</strong></p> <p>In this post i describe server side and client side code for multiple file upload.</p> <p>Following code is for mentioning the multipart data in appContext.xml</p> <p><strong>appContext.xml</strong></p> <pre><code>&lt;bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"&gt; &lt;!-- one of the properties available; the maximum file size in bytes --&gt; &lt;property name="maxUploadSize" value="20971520"/&gt; &lt;/bean&gt; </code></pre> <p><strong>Simpleupload.jsp:</strong></p> <p>script for multiple file upload:</p> <pre><code>&lt;script type="text/javascript"&gt; var totalsizeOfUploadFiles = 0; function getFileSizeandName(input) { var select = $('#uploadTable'); for(var i =0; i&lt;input.files.length; i++) { var filesizeInBytes = input.files[i].size; var filesizeInMB = (filesizeInBytes / (1024*1024)).toFixed(2); var filename = input.files[i].name; //alert("File name is : "+filename+" || size : "+filesizeInMB+" MB || size : "+filesizeInBytes+" Bytes"); if(i&lt;=4) { $('#filetd'+i+'').text(filename); $('#filesizetd'+i+'').text(filesizeInMB); } else if(i&gt;4) select.append($('&lt;tr id=tr'+i+'&gt;&lt;td id=filetd'+i+'&gt;'+filename+'&lt;/td&gt;&lt;td id=filesizetd'+i+'&gt;'+filesizeInMB+'&lt;/td&gt;&lt;/tr&gt;')); totalsizeOfUploadFiles += parseFloat(filesizeInMB); $('#totalsize').text(totalsizeOfUploadFiles.toFixed(2)+" MB"); if(i==0) $('#filecount').text("1file"); else { var no = parseInt(i) + 1; $('#filecount').text(no+"files"); } } } function CloseAndRefresh() { opener.location.reload(true); self.close(); } &lt;/script&gt; </code></pre> <p>html form design:</p> <pre><code>&lt;body&gt; &lt;form method="post" id="uploadForm" action="upload" enctype="multipart/form-data"&gt; &lt;table class="span10"&gt; &lt;tr&gt; &lt;td colspan="3"&gt; &lt;legend&gt;Simple Upload&lt;/legend&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;input type="file" name="files[]" multiple="multiple" onchange="getFileSizeandName(this);"/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="3"&gt; &lt;div id="uploaddiv"&gt; &lt;table id="uploadTable" class="table table-striped table-bordered"&gt; &lt;tr&gt; &lt;th&gt;Title&lt;/th&gt; &lt;th&gt;Size&lt;/th&gt; &lt;/tr&gt; &lt;tbody id="tbodyid"&gt; &lt;tr id="tr0"&gt; &lt;td id="filetd0" height="10px" width="50px"&gt;&lt;/td&gt; &lt;td id="filesizetd0" height="10px" width="5px"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr id="tr1"&gt; &lt;td id="filetd1"&gt;&lt;/td&gt; &lt;td id="filesizetd1"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr id="tr2"&gt; &lt;td id="filetd2"&gt;&lt;/td&gt; &lt;td id="filesizetd2"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr id="tr3"&gt; &lt;td id="filetd3"&gt;&lt;/td&gt; &lt;td id="filesizetd3"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr id="tr4"&gt; &lt;td id="filetd4"&gt;&lt;/td&gt; &lt;td id="filesizetd4"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;tfoot&gt; &lt;tr&gt; &lt;td id="filecount"&gt;&lt;/td&gt;&lt;td id="totalsize"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tfoot&gt; &lt;/table&gt; &lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="3"&gt; &lt;button class="btn btn-primary" type="submit" id="startButton" onClick="CloseAndRefresh();"&gt;Start&lt;/button&gt; &lt;button class="btn" id="cancelButton"&gt;Cancel&lt;/button&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; </code></pre> <p></p> <p>UploadController.java code:</p> <pre><code>@RequestMapping(value = "/upload", method = RequestMethod.POST) public void UploadReceipts(@RequestParam("files[]") List&lt;MultipartFile&gt; file) throws Exception { logger.info(" Inside the upload receipts method "+file.size()); for(int i=0; i&lt; file.size(); i++) { if(!file.get(i).isEmpty()) { CommonsMultipartFile cm = (CommonsMultipartFile) file.get(i); logger.info(" Inside the file upload method "+cm.getOriginalFilename()); simpleUploadService.uploadFileandSaveReceipt(cm); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload