Java Nice Day!

A programmer’s attempt in the blogging world

Saving images as blob in the database using Grails

Handling images is not as easy as handling simple http parameters. This is because images are received as binary objects that you can’t directly put in the database field. Here’s how i handled saving images to the database:

First, the form containing the image file input field should be declared as multipart:

As i understand it, we need to specify multipart because you can’t mix the text parameters with the binary parameters, so we should have it as multipart.

Of course, the file field inside the form should exist, for user to input the image file, e.g.:

<input id="screenshot" name="screenshot" type="file" />

Ok. now, in the controller, say ImageController, we can process the binary image this way:

def record = new Record() = params
if(request instanceof MultipartHttpServletRequest) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
CommonsMultipartFile file = (CommonsMultipartFile)multiRequest.getFile("screenshot");
record.screenshot = file.bytes // screenshot is the image blob field of the record
} // create the record

On save, the record should be created with the image stored as blob.
You might encounter some problem with the image field in the groovy file. So i suggest that you change the field into byte[] instead of Blob. i.e. in Record.groovy above,:

class Record {
// ..
// some fields
// ..
byte[] screenshot // declare the image as byte[] instead of blob
// ..
static constraints = {
// ..
screenshot(maxSize:1073741824) // max 4GB file

If you don’t do the procedure above to your groovy domain class, there might be a problem with saving the image to the blob field in the database.

Next post: how to display an image file from the database to the web page.

January 8, 2009 - Posted by | Groovy on Grails | , ,


  1. Where does “def record = new Record() = params ….” go in the controller? Which action save or create or perhaps other?

    Comment by Ed | February 16, 2009

  2. Ed: you can place it on the save action, which saves the record in the database. On the other hand, i think directly calling “ = params” will already save the image, as long as the request is a multipart request. try it out. 😉

    Comment by kerwintang | February 17, 2009

  3. Thanks for quick response. I will give it a try.

    Comment by Ed | February 17, 2009

  4. def mascota = {
    def mascotaInstance = new Mascota() = params
    if(request instanceof MultipartHttpServletRequest) {
    MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
    CommonsMultipartFile file = (CommonsMultipartFile)multiRequest.getFile(“foto”);
    record.screenshot = file.bytes
    } // create the record
    } //works!!!!! thanks

    Comment by Felipe | January 28, 2010

  5. […] Como guardar imagen en base de datos usando Grails 2 Julio, 2010 Posted by dorianruiz in Uncategorized. trackback Pues buscando algo en que entretenerme me dió curiosidad por saber como guardar una imagen en una base datos usando GRAILS je y encontré esto. […]

    Pingback by Como guardar imagen en base de datos usando Grails « Blog de Dorian Ruiz. | July 2, 2010

  6. I found this error: No such property: params for class: fidsadm.AddLogoMasterComposer. I use Zkoss for web GUI. can you tell me what is going wrong? thank you

    Comment by Toni | July 27, 2010

  7. Toni, it means you have a class “AddLogoMasterComposer” and you’re using a variable named “params” that you haven’t defined anywhere. 🙂

    Comment by kerwintang | July 27, 2010

  8. thank you so much – this really helped me!

    Comment by Dave | February 3, 2011

  9. Simply desire to say your article is as astounding.
    The clearness in your post is simply cool and i could assume you’re an expert on this subject. Fine with your permission let me to grab your feed to keep up to date with forthcoming post. Thanks a million and please continue the rewarding work.

    Comment by | April 24, 2013

  10. I’m curious to find out what blog system you happen to be using?
    I’m having some minor security problems with my latest blog and I would like to find something more safeguarded.
    Do you have any recommendations?

    Comment by Swagbucks Hack | December 23, 2013

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: