Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple File Upload in Grails

I'm trying to integrate the excellent jquery-file-upload from blueimp into Grails 2.0 and to write a new plugin for that. I know there is already a plugin, but it doesn't use resources and it hasn't been updated for 9 months, and jquery-file-upload has changed a lot in that time.

So I put all of the files of the distribution in my plugin, and now I'm trying to write a controller action that can handle the upload. The problem is that request.getFileNames() only gives me one files[] entry and I don't know how to retrieve the 3 individual files I am uploading.

How do you handle the files sent by an input like this: <input type="file" name="files[]" multiple>?

like image 760
Sebastien Avatar asked Apr 14 '12 17:04

Sebastien


2 Answers

Actually I figured it out. There is only one input[type=file] in the form and if I select several files they are not sent on the same request. The query-file-upload script sends as many POST requests as there are files. That's why request.getFileNames() only gives me one entry each time. I managed to create my plugin, it will be published soon. Watch out for http://grails.org/plugin/bootstrap-file-upload.

like image 91
Sebastien Avatar answered Nov 02 '22 08:11

Sebastien


request.getFileNames returns an array - you can iterate over this array to obtain all the files. The example is already there on the plugin description.

def all = request.getFileNames()
all.each {name ->
   def file = request.getFile(name)
}

This should give you all the files that were uploaded.

like image 26
Sudhir N Avatar answered Nov 02 '22 08:11

Sudhir N