Serving local images with Play 2 / Scala

I am trying to figure out a template for downloading and serving files (images) locally. I calculated the download part, but got a little confused in the repository and applied.

I am confused about how to display locally stored images on one page using "Ok.sendFile". How to associate it with "img src" tags on a view? Another option I could think of is to run a (separate) web server locally only for storing files, which does not make much sense.

+4
source share
1 answer

Just add an action to the 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 appropriate route to your routes file:

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

And your HTML should look like this:

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

or if you use Scala templates:

 <img src="@routes.controllers.MyPictureController.picture("image.png")"> 
+9
source

Source: https://habr.com/ru/post/1496005/


All Articles