views:

48

answers:

1

Hi,

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

+1  A: 

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:

Rob Hruska