Marcos
Marcos

Reputation: 73

Cannot render blob as image in grails 2.1

I'm trying to render an uploaded image from a gsp without success. I have this to render the image.. What is wrong?

//domain-class
class Something {
    ...
    byte[] image
}

//controller
def displayImage(){
    def something = Something.get(id)
    response.contentType = 'image/jpeg'
    response.outputStream << something?.image
    response.outputStream.flush()
}

//gsp
<img src="${createLink(action:'displayImage',  id:something?.id)}" />

I've modified the controller to be like this

displayImage(){
    def something = Something.get(params.id)
    ........
}

I don't get an exception but neither get the image displayed. I don't know if this has something to do with the ContentType or any other thing that i have to specify. Any ideas??

Upvotes: 0

Views: 980

Answers (3)

Chetan Sharma
Chetan Sharma

Reputation: 44

def displayImage(){
    Photo photo = Photo.get(1)
    response.setHeader("Content-disposition", "attachment; filename=${photo?.name}")
    response.contentType = photo?.type
    response.outputStream << new File(photo?.path).getBytes()
    response.outputStream.flush()
    return;
}

Upvotes: 0

rsijaya
rsijaya

Reputation: 169

try this :

def displayImage(){
def something = Something.get( params.id )
byte[] image = something.image
response.outputStream << image
}

Upvotes: 0

dmahapatro
dmahapatro

Reputation: 50245

You need to go with either of the below approaches:

def displayImage(){
    def something = Something.get(params.id)
    ........
}

or

def displayImage(Long id){
    .....
}

Controller has params bind automatically instead of id. If you get an exception after having this modification as well, then please add that to the question.

Upvotes: 1

Related Questions