To upload a file, you must install enctype on the form. To do this, you can use <g:uploadForm> , which is identical to the standard form tags, except that it automatically sets the enctype attribute to "multipart / form-data".
I prefer to use the Grails Selfie Plugin plugin to upload images / files to attach files to your domain models, upload to CDN, check contents or create thumbnails.
Domain
import com.bertramlabs.plugins.selfie.Attachment class Book { String name Attachment photo static attachmentOptions = [ photo: [ styles: [ thumb: [width: 50, height: 50, mode: 'fit'], medium: [width: 250, height: 250, mode: 'scale'] ] ] ] static embedded = ['photo']
Gsp
<g:uploadForm name="myUpload" controller="upload" action="updateStatus"> <input type="file" name="myFile" /> </g:uploadForm>
controller
class PhotoController { def upload() { def photo = new Photo(params) if(!photo.save()) { println "Error Saving! ${photo.errors.allErrors}" } redirect view: "index" } }
Sources
1. uploadFrom
2. selfie plug
Tyler rafferty
source share