The result types are in play.api.mvc.Results
, see here on GitHub.
In order to add headers, you'd write:
Ok
.withHeaders(CONTENT_TYPE -> "application/octet-stream")
.withHeaders(CONTENT_DISPOSITION -> "attachment; filename=foo.txt")
or
Ok.withHeaders(
CONTENT_TYPE -> "application/octet-stream",
CONTENT_DISPOSITION -> "attachment; filename=foo.txt"
)
And here is a full sample download:
def download = Action {
import org.apache.commons.io.IOUtils
val input = Play.current.resourceAsStream("public/downloads/Image.png")
input.map { is =>
Ok(IOUtils.toByteArray(is))
.withHeaders(CONTENT_DISPOSITION -> "attachment; filename=foo.png")
}.getOrElse(NotFound("File not found!"))
}
To download a file, Play now offers another simple way:
def download = Action {
Ok.sendFile(new java.io.File("public/downloads/Image1.png"), fileName = (name) => "foo.png")
}
The disadvantage is that this results in an exception if the file is not found. Also, the filename is specified via a function, which seems a bit overkill.
Encode your optional parameters as Option[String]
(or Option[java.util.Date]
, but you’ll have to implement your own QueryStringBindable[Date]
):
def birthdays(from: Option[String], to: Option[String]) = Action {
// …
}
And declare the following route:
GET /birthday controllers.Application.birthday(from: Option[String], to: Option[String])
Best Answer
You should be able to do the following:
And also things like: