开发者

how to upload multiple files in grails

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 -&g开发者_开发问答t; println("${obj}"); } I only got the first one ..


request.getMultiFileMap().documentFile.each {
    println it.originalFilename
}


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)  
 }  


You try with this one

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜