3

有什么办法可以将数据库中的图像路径显示在视图页面上,以及如何使用 playframework2.0 在视图页面中显示图像

4

1 回答 1

3

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)
于 2013-05-28T07:08:08.983 回答