Turar
Turar

Reputation: 1561

Serving local images with Play 2 to be displayed inline

Earlier I asked how to serve a local image here: Serving local images with Play 2 / Scala

This works well, except that the image is forced to download by the browser, instead of being displayed in the browser.

I have added "inline = true" argument to OK.sendFile, as described here: http://www.playframework.com/documentation/2.2.x/ScalaStream , but that doesn't seem to work, the image is still being downloaded. It seems that Content Type is not getting recognized? Play documentation says it should automatically set it using the file extension, so not sure what's going. Help?

EDIT: Response header example:

HTTP/1.1 200 OK

Content-Length:2694220

Content-Type: application/octet-stream

Upvotes: 0

Views: 438

Answers (1)

Turar
Turar

Reputation: 1561

Looking through Play's code, I realized I could simply add .withHeaders onto sendFile:

Ok.sendFile(content = new java.io.File("filepath"), inline = true).withHeaders(CONTENT_TYPE -> "image/jpeg")

This solved the problem! Thanks @biesior for the pointer.

Upvotes: 2

Related Questions