Reputation: 29
My users need to download a file when hitting a certain controller in my Play application. I thought this would do the trick:
def downloadFile = Action {
Ok.sendFile(new File("example.zip"))
}
But it seems to only give the actual content of the file instead of downloading the file. Could anybody tell me what I'm doing wrong?
Thanks
Upvotes: 0
Views: 2402
Reputation: 29
Turns out the REST client we are using is automatically converting the file straight into its content instead of letting us download the file. Hitting it from a normal browser works as intended.
Upvotes: 0
Reputation: 3107
Try this instead:
def index = Action {
Ok.sendFile(
content = new java.io.File("/tmp/fileToServe.pdf"),
fileName = _ => "termsOfService.pdf"
)
}
Its from the documentation itself.
Now you don’t have to specify a file name since the web browser will not try to download it, but will just display the file content in the web browser window. This is useful for content types supported natively by the web browser, such as text, HTML or images.
See this: https://www.playframework.com/documentation/2.0/ScalaStream
Upvotes: 1