ps0604
ps0604

Reputation: 1081

Specify MIME type when downloading binary file with OK() in Play for Scala

I download a binary file using Play for Scala like so:

class Test extends Controller  {

    def test = Action { request =>

        val byteArray = Files.readAllBytes(Paths.get("path/to/file.jpeg"))
        Ok(byteArray)

    }

 }

But I need to define the MIME file type, how to do that?

UPDATE

I tried with as and sendFile but in both cases Play returns a json format, any ideas what's wrong?

enter image description here

Upvotes: 1

Views: 537

Answers (1)

Andrey Tyukin
Andrey Tyukin

Reputation: 44992

The Ok (which is a Status defined in Results) has the method as that allows to set the MIME type:

Ok(byteArray).as("image/jpeg")

Note that there is also sendFile helper.

Upvotes: 2

Related Questions