Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate over uploaded files in Grails

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

like image 432
nightingale2k1 Avatar asked Sep 14 '10 15:09

nightingale2k1


2 Answers

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:

  • http://www.grails.org/File+Upload
  • http://www.grails.org/doc/latest/guide/6.%20The%20Web%20Layer.html#6.1.9%20Uploading%20Files
like image 64
Rob Hruska Avatar answered Oct 22 '22 09:10

Rob Hruska


Starting with grails 1.2 (spring 3.0), you can access multiple files from inputs with the same name (or that use the HTML5 multiple attribute) with the multiFileMap property of the controller's request object (when the request is a multipart form post, the request object will be an instance of MultipartRequest). So you can access the list of MultipartFile objects for a specific input name (ie documentFile) like this:

def save = {
    List<MultipartFile> files = request.multiFileMap.documentFile
    int count = files.findAll { !it.empty }.size
    render "uploaded $count files"
}

Or access all MultipartFile objects from all inputs like this:

def save = {
    List<MultipartFile> files = request.multiFileMap.collect { it.value }.flatten()
    int count = files.findAll { !it.empty }.size
    render "uploaded $count files"
}

Edit 2013-02-08: Brian Adams asks:

In above case, I can access all MultipartFile objects from all inputs on my web page. But I wants to get files from any input is multiple. Ex: I have input name is: "uploadFiles", and I wants get all files from this input tag any. Can you help me?

Brian, I think what you want is the first version of the above. If your file input is named "uploadFiles", then you can access it from the multiFileMap using the "uploadFiles" key:

def save = {
    List<MultipartFile> files = request.multiFileMap.uploadFiles
    int count = files.findAll { !it.empty }.size
    render "uploaded $count files"
}
like image 7
Justin Ludwig Avatar answered Oct 22 '22 08:10

Justin Ludwig