Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring file upload with two file upload elements
    text
    copied!<p>I have a form with two file upload elements and those are supporting for multiple files upload. My question is how can I get the data of both file upload elements.</p> <p>Eg: I need fields of both <strong>"files"</strong> and <strong>"files2"</strong> elements.</p> <p>With the following code I am only able to get the details of one file upload element. I am new to Spring and hope my question is clear.</p> <p><img src="https://i.stack.imgur.com/erfpL.png" alt="enter image description here"> </p> <pre><code>&lt;table id="fileTable"&gt; &lt;tr&gt; &lt;td&gt;&lt;label for="file"&gt;Image URL&lt;/label&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="file" name="files[0]" id="file"&gt;&lt;/input&gt;&lt;/td&gt; &lt;td&gt;&lt;input id="addFile" type="button" value="Add Image URL" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr id="img_hover"&gt; &lt;td&gt;&lt;label for="file2"&gt;Image Hover URL&lt;/label&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="file" name="files2[0]" id="file2"&gt;&lt;/input&gt;&lt;/td&gt; &lt;td&gt;&lt;input id="addFile2" type="button" value="Add Image Hover URL" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2"&gt; &lt;input type="submit" value="Save"/&gt; &lt;/td&gt; &lt;/tr&gt; </code></pre> <p><br> </p> <pre><code>@RequestMapping(value = "/save.action", method = RequestMethod.POST) public ModelAndView save(HttpServletRequest request, HttpServletResponse response, @ModelAttribute("uploadForm") FileUploadForm uploadForm ) { List&lt;MultipartFile&gt; files = uploadForm.getFiles(); List&lt;String&gt; fileNames = new ArrayList&lt;String&gt;(); if (null != files &amp;&amp; files.size() &gt; 0) { for (MultipartFile multipartFile : files) { String fileName = multipartFile.getOriginalFilename(); fileNames.add(fileName); System.out.println(fileName); } } return new ModelAndView("index", "model", ""); } </code></pre>
 

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