Posts Tagged ‘file upload’
A Simple Grails Controller for File Management
Using Grails as a CMS, often times you want your users to add and remove files to a page similar to the way assets are attached to a page in the Confluence Wiki. I recently had to write a controller to handle files upload and download, and thought it might be interesting to share this since it is done so often in the Grails world.
Download the controller : FileResourceController.groovy
Download the view : list.gsp
Download complete sample app : uploader.zip
( to run: install grails. Invoke grails run-app. Navigate to http://localhost:8080 )
image uploading in Grails with 5 lines of code
In your domain class, define
class Something{
byte[] image
}
in your controller, call
def image= {
def something = Something.get( params.id )
byte[] image = something.image
response.outputStream << image
}
Image gets copied to the database. You can then show it by
<img src="${createLink(controller:'something', action:'image', id: something.id)}"/>

