Serving local images with Play 2 / Scala

后端 未结 1 799
礼貌的吻别
礼貌的吻别 2020-12-30 16:19

I\'m trying to figure out a pattern for uploading and serving files (images) locally. I figured out the upload part, but a little confused on the storage and serve part.

相关标签:
1条回答
  • 2020-12-30 16:58

    Just add an Action in a Controller that provides the image:

    def picture(name: String) = Action {
    
       Ok.sendFile(new java.io.File(name)) // the name should contains the image extensions
    }
    

    Then add the corresponding route in your routes file:

    GET /picture/:name  controllers.MyPictureController.picture(name: String)
    

    And your HTML should look like:

    <img src="/picture/image.png">
    

    or if you use the Scala templates:

    <img src="@routes.controllers.MyPictureController.picture("image.png")">
    
    0 讨论(0)
提交回复
热议问题