图像显示使用播放框架和scala

时间:2013-05-28 06:01:19

标签: scala view playframework

是否存在数据库中的图像路径并在视图页面上显示以及如何使用playframework2.0在视图页面中显示图像

1 个答案:

答案 0 :(得分:2)

HTML:

@(name:String)
    <img class="myClass" alt="myAlt" src="@routes.Application.image(name)">

控制器:

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
          ......
        }
    }

路线:

GET /images/:name controllers.Application.image(name: String)