[2.0] Rendering a java.sql.Blob object as an image in a view

239 views
Skip to first unread message

Dombou

unread,
Mar 25, 2012, 1:03:11 AM3/25/12
to play-fr...@googlegroups.com
Hi guys,

I have a model to my view that contains a java.sql.Blob object. I want to render this Blob object as an image. 

The code for the view looks like this:

@(image: Option[models.market.Image])(implicit flash: play.api.mvc.Flash)
@main {
    @image.get.id
    @image.get.data.length
        <img height="100" width="200" src="@routes.Market.getImageById(image.get.id.get)" />
}

That code works, but notice how I have actually had to provide a separate controller - and thus a separate router - for the handling of an image. Is it possible to just call a function that returns a binary stream or something?

I've searched through this group, StackOverflow etc and I see a lot of people doing this: 
<img height="100" width="200" src="@{Market.getImageById(image.get.id.get)}" />

But it doesn't work for me. 

Pascal Voitot Dev

unread,
Mar 25, 2012, 4:50:44 AM3/25/12
to play-fr...@googlegroups.com
you can call anything from Scala Templates as you can import any classes from any package.

Yet in your case, an IMG requires a SRC which is an URL.
So you need to be able to access an url streaming the data of the image.
An action in a controller is certainly the best way to do it in your case, isn't it?

Pascal

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/mJzolE1xDG0J.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

Nullable

unread,
Mar 25, 2012, 2:12:03 PM3/25/12
to play-fr...@googlegroups.com
The other one you got is actually completely equivalent to the first one, except for that it uses the old Groovy-based templates.

Dominic Bou-Samra

unread,
Mar 25, 2012, 6:00:31 PM3/25/12
to play-fr...@googlegroups.com
So this is the correct and preferred way to render images from a database? 

Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/dglO2shttIAJ.

Jacob Groundwater

unread,
Mar 26, 2012, 9:56:47 PM3/26/12
to play-fr...@googlegroups.com
I think, are you asking, if you can embed the image directly into the HTML page?

Yes, with caveats. For example see (quick google search) 
In such a case, you would not need a separate controller for the images since the browser is not making a second request.

On Monday, March 26, 2012 6:00:31 AM UTC+8, Dombou wrote:
So this is the correct and preferred way to render images from a database? 

Sent from my iPhone

On Mar 26, 2012, at 4:12 AM, Nullable <teo...@gmail.com> wrote:

The other one you got is actually completely equivalent to the first one, except for that it uses the old Groovy-based templates.

On Sunday, March 25, 2012 7:03:11 AM UTC+2, Dombou wrote:
Hi guys,

I have a model to my view that contains a java.sql.Blob object. I want to render this Blob object as an image. 

The code for the view looks like this:

@(image: Option[models.market.Image])(​implicit flash: play.api.mvc.Flash)
@main {
    @image.get.id
    @image.get.data.length
        <img height="100" width="200" src="@routes.Market.​getImageById(image.get.id.get)​" />
}

That code works, but notice how I have actually had to provide a separate controller - and thus a separate router - for the handling of an image. Is it possible to just call a function that returns a binary stream or something?

I've searched through this group, StackOverflow etc and I see a lot of people doing this: 
<img height="100" width="200" src="@{Market.getImageById(​image.get.id.get)}" />

But it doesn't work for me. 

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/dglO2shttIAJ.
To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages