如何迭代在Grails中上传的文件 [英] How to iterate over uploaded files in Grails

查看:131
本文介绍了如何迭代在Grails中上传的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个GSP:

 < g:uploadForm name =myFormaction ='save'> 
< input type ='file'name ='documentFile'value =''/>
< input type ='file'name ='documentFile'value =''/>
< input type ='file'name ='documentFile'value =''/>
< input type ='file'name ='documentFile'value =''/>
< input type ='submit'value ='提交'/>
< / g:uploadForm>

但是当我试图通过输入来查看控制器中的结果时:

  render(params); 
返回true;

我得到了这个结果:

<$ p $ documentFile:org.springframework.web.multipart.commons.CommonsMultipartFile@14dcf95

如何读取每个正在上传的文件?
可以得到以下内容吗?

$ p $ documentFile:[File,null,File,null] //第二和第四不被使用)

ps:我正在使用grails 1.2.2 <首先,您需要为每个文件输入提供唯一的名称:
$ b

解决方案 $ b

 < g:uploadForm name =myFormaction =save> 
< input type =filename =documentFile1value =/>
< input type =filename =documentFile2value =/>
...
< / g:uploadForm>

然后在您的控制器中,您可以使用:

  //通过名称访问每个文件
File file = request.getFile('documentFile1')

//遍历它们
request.fileNames.each {
File file = request.getFile(it)
}

我很确定您的名称属性必须是唯一的。我在API中找不到任何东西,它们将允许您获取使用相同输入名称上传的文件数组。



参考文献:


I have this GSP:

<g:uploadForm name="myForm" action='save'>    
    <input type='file' name='documentFile' value=''/>
    <input type='file' name='documentFile' value=''/>
    <input type='file' name='documentFile' value=''/>
    <input type='file' name='documentFile' value=''/>
    <input type='submit' value='Submit'/>
</g:uploadForm>

But when I tried to view the result in controller by typing:

render(params);
return true;

I got this result:

"documentFile":org.springframework.web.multipart.commons.CommonsMultipartFile@14dcf95

How do I read each file that is being uploaded? Could I get the following?

documentFile:[File,null,File,null] // (if the 2nd and the 4th are not being used)

ps: I'm using grails 1.2.2

解决方案

First, you'll need to give unique names to each of your file inputs:

<g:uploadForm name="myForm" action="save">
    <input type="file" name="documentFile1" value=""/>
    <input type="file" name="documentFile2" value=""/>
    ...
</g:uploadForm>

Then in your controller, you can use:

// access each file by name
File file = request.getFile('documentFile1')

// or iterate through them
request.fileNames.each {
    File file = request.getFile(it)
}

I'm pretty sure that your name attributes have to be unique. I can't find anything in the API that will allow you to get an array of files that were uploaded with the same input name.

References:

这篇关于如何迭代在Grails中上传的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆