如何在grails中上传多个文件

nig*_*2k1 6 grails file-upload

我有一个包含像这样的多文件上传的表单

<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>
Run Code Online (Sandbox Code Playgroud)

如果需要,用户可以添加更多...如何使用迭代器获取每个文件?

如果我只使用一个文件,request.getFile('documentFile'); 但如果我尝试过,request.getFileNames().each{obj -> println("${obj}"); } 我只得到第一个...

Cas*_*dim 8

request.getMultiFileMap().documentFile.each {
    println it.originalFilename
}
Run Code Online (Sandbox Code Playgroud)

  • 这是使用 input 标签的 multiple 属性时的正确解决方案。 (2认同)

Jin*_*ekh 5

你想这样做

   <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>  
Run Code Online (Sandbox Code Playgroud)

在您的控制器中

def files = []  
params.documentFile.each {  
  files.add(it.value)  
 }  
Run Code Online (Sandbox Code Playgroud)