How to display image in grails GSP


I'm still learning Grails and seem to have hit a stumbling block.

Here are the 2 domain classes:

class Photo {
    byte[] file 

    static belongsTo = Profile

class Profile {
    String fullName
    Set photos

    static hasMany = [photos:Photo]     

The relevant controller snippet:

class PhotoController {

    def viewImage = {

      def photo = Photo.get( )
      byte[] image = photo.file 
      response.outputStream << image


Finally the GSP snippet:

<img class="Photo" src="${createLink(controller:'photo', action:'viewImage', id:'')}" />

Now how do I access the photo so that it will be shown on the GSP? I'm pretty sure that is not correct.

Best Answer

If you have a url for the image, you just have to make sure you return the appropriate anser in the controller:

  def viewImage= {
    //retrieve photo code here
    response.setHeader("Content-disposition", "attachment; filename=${}")
    response.contentType = photo.fileType //'image/jpeg' will do too
    response.outputStream << photo.file //'myphoto.jpg' will do too