Sharing the Code

Programming stuff that might be useful to others

Saving images to a database in Grails

A domain class that contains an image (or any other binary data) would look something like this:

[sourcecode language=”java”]
class Image {
byte[] data
String contentType
}
[/sourcecode]

In MySQL the data column in the database will be a tinyblob which is generally too small for images and you will probably get the following error:

com.mysql.jdbc.MysqlDataTruncation: Data to long for column

To force it to use something bigger you would add a maxSize constraint to the data property:

[sourcecode language=”java”]
class Image {
byte[] data
String contentType

static constraints={
//maxSize: 200K
data(maxSize:204800)
}
}
[/sourcecode]

Comments are closed