7

I have a form that contains multi file upload like this one

<g:form name="legalActionForm" controller="legalAction" action="save" enctype="multipart/form-data">
<input type='file' name='documentFile'/> <input type='text' name='documentDate'/>
<input type='file' name='documentFile'/> <input type='text' name='documentDate'/>
<input type='file' name='documentFile'/> <input type='text' name='documentDate'/>
<input type='file' name='documentFile'/> <input type='text' name='documentDate'/>
<input type='file' name='documentFile'/> <input type='text' name='documentDate'/>
<input type='submit' value='update'/>
</g:form>

user can add more if needed ... how to get each file using iterators ?

if only one file I use request.getFile('documentFile'); but if I tried with request.getFileNames().each{obj -> println("${obj}"); } I only got the first one ..

3
  • 2
    possible duplicate of How to iterate over uploaded files in Grails Commented Dec 10, 2010 at 16:41
  • The reason you only received the first 'documentFile' when using request.fileNames.each {} is because your names are not unique in your form. Jinesh's answer fixes this; I also addressed this when you asked this question a couple months ago. Commented Dec 10, 2010 at 16:43
  • @nightingale2k1 new answer added Commented Mar 25, 2014 at 8:58

3 Answers 3

9
request.getMultiFileMap().documentFile.each {
    println it.originalFilename
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is the correct solution when using the multiple attribute of the input tag.
5

You want to do it like so

   <g:form action="save" method="post" enctype="multipart/form-data" >  
   <input type='file' name='documentFile.1' />  
   <input type='file' name='documentFile.2' />  
   <input type='file' name='documentFile.3' />  
   </g:form>  

In your controller

def files = []  
params.documentFile.each {  
  files.add(it.value)  
 }  

1 Comment

When I use the multiple attribute I can't get it to work. <input type="file" id="files" name="arquivos[]" multiple />
3

You try with this one

request.fileNames.each {
   MultipartFile file = request.getFile(it)
   //File file = request.getFile(it)
   //do what you want
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.