Is there any way to the path of image in the database and display it on view page and how to display image in view page using playframework2.0
Asked
Active
Viewed 1,338 times
1 Answers
3
html:
@(name:String)
<img class="myClass" alt="myAlt" src="@routes.Application.image(name)">
Controller:
object Application extends Controller {
def image(name:String) = Action {
val MimeType = "image/png"
try {
val imageData: Array[Byte] = fetchImageFromDatabase(name)
Ok(imageData).as(MimeType)
}
catch {
case e: IllegalArgumentException =>
BadRequest("Couldn’t generate image. Error: " + e.getMessage)
}
}
def fetchImageFromDatabase(name: String): Array[Byte] = {
//import java.io.ByteArrayOutputStream
//import java.awt.image.BufferedImage
......
}
}
routes:
GET /images/:name controllers.Application.image(name: String)

Andrzej Jozwik
- 14,331
- 3
- 59
- 68
-
...thnx!... the fetchImageFromDatabase function for what use and please be expand that method – May 28 '13 at 07:13
-
1You mention about database - so you have to read it from database and convert to byte array – Andrzej Jozwik May 28 '13 at 07:18
-
my database table name :post and the fields are id,name and image – May 28 '13 at 07:20
-
1See [Easiest way to convert a Blob into a byte array](http://stackoverflow.com/a/6662933/651140) – Andrzej Jozwik May 28 '13 at 07:44