gpt4 book ai didi

scala - 在 Play2 Scala 中使用 reactivemongo 显示来自 MongoDB 的图像

转载 作者:行者123 更新时间:2023-12-02 00:11:06 24 4
gpt4 key购买 nike

我将此列表传递给 Play2 模板:

files: Option[List[(String, reactivemongo.api.gridfs.ReadFile[reactivemongo.bson.BSONValue])]])

如何从列表中拉出第一张图片并将该图片渲染到 html 标签

<img src=" ">

任何帮助都会很棒 :)

谢谢丹尼

最佳答案

从查看响应式 mongo 演示应用程序来看,您似乎必须添加一条路由以从 gridfs 提供图像。看route filecontroller ,该示例显示了如何提供附件。所以你的情况可能看起来像这样:

在路由文件中:

GET     /img/:id            controllers.Images.getImg(id)

Images Controller 中:

def getImg(id: String) = Action {
Async {
import reactivemongo.api.gridfs.Implicits.DefaultReadFileReader
val file = gridFS.find(BSONDocument("_id" -> new BSONObjectID(id)))
serve(gridFS, file)
}
}

然后在你的模板中:

@if(files.isDefined) {
@files.get.map { file =>
<img src="@routes.Images.getImg(file._1)"/>
}
}

关于scala - 在 Play2 Scala 中使用 reactivemongo 显示来自 MongoDB 的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15341608/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com