如何用play 2.0渲染二进制文件? [英] How to render a binary with play 2.0?
本文介绍了如何用play 2.0渲染二进制文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我被困在一个明显的地方:
I am stuck on an obvious one:
如何使用Play 2.0从控制器渲染图像?
How to render an image from a controller using Play 2.0 ?
在播放1.0中,有一个renderBinary()
方法.现在不见了.
With play 1.0 there was a renderBinary()
method. It is now gone.
Play-RC1仅定义了3种内容类型:Txt,Html和Xml....
Play-RC1 only defined 3 content types: Txt, Html and Xml....
因此,如何从控制器提供二进制文件?
推荐答案
在具有Play 2.x的Scala中,代替renderBinary()
或Binary()
只是使用
In Scala with Play 2.x, instead of renderBinary()
or Binary()
juste use
Ok(byteArray).as(mimeType)
在前面的示例中,给出了:
In the previous example, this gives:
import play.api._
import play.api.Play.current
import play.api.mvc._
object Application extends Controller {
def index = Action {
val app = Play.application
var file = Play.application.getFile("pics/pic.jpg")
val source = scala.io.Source.fromFile(file)(scala.io.Codec.ISO8859)
val byteArray = source.map(_.toByte).toArray
source.close()
Ok(byteArray).as("image/jpeg")
}
}
希望这会有所帮助.
这篇关于如何用play 2.0渲染二进制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文