How to download a file with Play Framework 2.0

2019-03-12 10:23发布

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

3条回答
手持菜刀,她持情操
2楼-- · 2019-03-12 11:17

Serving files If it’s not a problem to load the whole content into memory for simple content what about a large data set? Let’s say we want to send back a large file to the web client.

read more at : http://www.playframework.com/documentation/2.0.x/JavaStream

查看更多
唯我独甜
3楼-- · 2019-03-12 11:21

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

查看更多
forever°为你锁心
4楼-- · 2019-03-12 11:27

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

查看更多
登录 后发表回答