1

I want to loop through this class and display in a table. what is the easiest way to display the image?

class Crop {
    static hasMany = [diseases: Disease]
    int id
    String commonName
    String scientificName
    byte[] image
}

static mapping = {
    table: 'Crops'
    commonName length : 100
    scientificName length: 100
    image sqlType: "longblob"
}
1
  • Normally you would do an extra request for each image which are handled by a controller. Do you really have to do this inside gsp? And maybe you should accept some of your earlier questions first? Commented Dec 22, 2012 at 10:15

2 Answers 2

2

You can if you don't mind the lack of browser compatablity use data: uri encoding:

<img src="data:image/png;base64,${crop.image.encodeBase64()}"/>

see the Wikipedia page on Data URI encoding scheme

@Xeon suggest the more conventional approach, ie create a controller that returns an image response with the correct mime type and the byte array as the body.

Sign up to request clarification or add additional context in comments.

Comments

1

You could do the same thing as in this answer.

Or you can create controller which provide image data stream from your entity. On GSP you could write:

<img src="${request.contextPath}/imageController/actionName?id=${entity.id}" ...

But this is unusual - storing images in entities as byte[]. You should consider changing it to String which would indicate a path/filename of the image.

1 Comment

I wouldn't say storing images in the DB is unusual, I've seen it used on numerous occasions, and to be honest is usual a good first choice for a lot of applications.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.