我是Play scala框架
的新手,在我的项目中我有上传
图像
和显示
的功能code> 将上传的图像放入 view
页面。我已将上传的图像
存储到系统的local
文件夹中,并传递图像path
进入我的 view
页面,但它不显示图像。
我已将图像存储位置 path
作为 imagepath
传递,并使用以下代码来查看图像。
<img <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d0a3a2b3ed90a2bfa5a4b5a3fe91a3a3b5a4a3feb1a4" rel="noreferrer noopener nofollow">[email protected]</a>(imagepath) class="responsive" >
注意:如果我将图像存储在projectname/public/images
项目目录
中,图像将显示在 View 页面中。请帮我解决这个问题。
最佳答案
您需要自己实现一项操作。正如我在Play Framework: Handling dynamic created files (images) in PRODUCTION mode中回答的那样
步骤如下 首先在您的路由文件中添加如下内容:
GET /user/images/:name controllers.Application.imageAt(name:String)
并在 imageAt 操作中编写一个简单的文件读取器,它返回流中的文件。同样,我的示例是用 Java 编写的,但您应该使用 Scala 归档相同的示例
File imageFile = new File(ReportFileHelper.getImagePath());
if (imageFile.exists()) {
//resource type such as image+png, image+jpg
String resourceType = "image+"+imageName.substring(imageName.length()-3);
return ok(new FileInputStream(imageFile)).as(resourceType);
} else {
return notFound(imageFile.getAbsoluteFile());
}
之后,可以通过反向路由器访问图像:
<img <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0b787968364b79647e7f6e78254a7b7b6762686a7f6264652562666a6c6e4a7f" rel="noreferrer noopener nofollow">[email protected]</a>(imageName)>
或者直接通过url
<img src="/user/images/imageName">
关于image - 如何在play scala项目中查看本地目录中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32241954/