scalaplayframeworkplayframework-2.5

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


Solution

  • 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.