Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download a file with Play Framework 2.0

I'm using Play Framework 2.0.3 to create an application which delivers Excel files that should be downloadable by the user.

 response().setContentType("application/x-download");  
 response().setHeader("Content-disposition","attachment; filename=tradeLogTest.xlsx");  

but,how to get the outputstream from response()?tks

like image 656
ma nicolas Avatar asked Dec 17 '12 15:12

ma nicolas


People also ask

How do I download a play framework?

Installation from the binary package In general, the installation instructions are as follows. Install Java. Download the latest Play binary package and extract the archive. Add the 'play' command to your system path and make sure it is executable.

How do I run play framework in Windows?

To run the Play framework, you need JDK 6 or later. If you are using MacOS, Java is built-in. If you are using Linux, make sure to use either the Sun JDK or OpenJDK (and not gcj, which is the default Java command on many Linux distros). If you are using Windows, just download and install the latest JDK package.


2 Answers

Play's action can return a File:

response().setContentType("application/x-download");  
response().setHeader("Content-disposition","attachment; filename=tradeLogTest.xlsx"); 
return ok(new File("/absolute/path/to/tradeLogTest.xlsx"));

Here's an API for Results

like image 200
biesior Avatar answered Oct 08 '22 21:10

biesior


Providing download option for static files can be done in Play as:

Ok.sendFile(new File("path to file/abc.csv"), inline=true).withHeaders(CACHE_CONTROL->"max-age=3600",CONTENT_DISPOSITION->"attachment; filename=abc.csv", CONTENT_TYPE->"application/x-download");

There are other parameters that are also available

For Internet Explorer - make sure you set the Content Disposition

like image 42
AJ.P Avatar answered Oct 08 '22 23:10

AJ.P