Monday, July 20, 2009

Displaying images from database records in Grails pages

Displaying images from database records in Grails is fairly simple, and is far easier than doing it in Java.

First, prepare a definition in your controller for the image display, e.g.:

def showImage = {
def record = Record.get( params.id ) // get the record
response.outputStream << record.screenshot // write the image to the outputstream
response.outputStream.flush()
}


Once you define the above, you should be able to access the url /(context-name)/record/showImage/(id) which will display the image of that record id.

In your gsp file, you can then display the image by simply specifying the source of the img file as the url above.

<img src="/(context-name)/record/showImage/${record.id}">


The image should be displayed by that code. You can adjust the height and width accordingly.

No comments:

Post a Comment