【问题标题】:How to view images from local directories in play scala project如何在play scala项目中查看本地目录中的图像
【发布时间】:2015-08-27 06:16:12
【问题描述】:

我是Play scala framework 的新手,在我的项目中,我有uploadingimageshowing 的功能,上传的图片到view 页面。我已将uploaded images 存储到我系统的@ 987654328@ 文件夹并将图像path 传递到我的view 页面,但它不显示图像。 我已将图像存储位置path 传递为imagepath 并使用以下代码查看图像。

<img src=@routes.Assets.at(imagepath) class="responsive" >

注意:如果我将图像存储在projectname/public/images 项目directory 中,图像将显示在视图页面中。请帮我解决这个问题。

【问题讨论】:

标签: image scala file playframework playframework-2.3


【解决方案1】:

您需要自己实施一个操作。正如我在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 src=@routes.Application.imageAt(imageName)>

或者直接通过网址

 <img src="/user/images/imageName">

【讨论】:

  • Waterscar,Thomas 和你的答案都是一样的。谢谢
猜你喜欢
  • 2013-04-12
  • 2013-07-19
  • 2022-01-18
  • 2020-08-17
  • 1970-01-01
  • 1970-01-01
  • 2015-08-27
  • 2019-06-03
  • 2014-03-01
相关资源
最近更新 更多