I am trying to upload a file in grails in my gsp I have:
In order to upload a file you must set the enctype on the form. To do so you can make use of the which is identical to the standard form tag except that it sets the enctype attribute to "multipart/form-data" automatically.
I prefer to make use of the Grails Selfie Plugin an Image / File Upload Plugin to attach files to your domain models, upload to a CDN, validate content, or produce 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'] //required
static constraints = {
photo contentType: ['image/jpeg','image/png'], fileSize:1024*1024 // 1mb
}
}
GSP
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 plugin