File download with ninja

327 views
Skip to first unread message

John Purnell

unread,
Dec 13, 2014, 11:59:28 PM12/13/14
to ninja-f...@googlegroups.com
How do I do a file download in ninja.

I have a File object and would like to stream it back like the answer above.

The file is text ( a GEDCOM file ) but large, so I could read it into a string and do a renderRaw but is there a better way?



Raphael André Bauer

unread,
Dec 15, 2014, 3:04:05 PM12/15/14
to ninja-f...@googlegroups.com
Hey John,

there is something similar in our integration tests here:
https://github.com/ninjaframework/ninja/blob/develop/ninja-servlet-integration-test/src/main/java/controllers/UploadController.java

It's of course not exactly what you are asking for. A stripped down
version would be that here:

Renderable renderable = new Renderable() {

@Override
public void render(Context context, Result result) {
ResponseStreams responseStreams = context.finalizeHeaders(result);
responseStreams.getOutputStream() // do what you want with the streams
}
};
return new Results.ok().render(renderable);

you can set the headers and everything else before calling "
context.finalizeHeaders(result);"

Cheers,

Raphael
> --
> You received this message because you are subscribed to the Google Groups
> "ninja-framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ninja-framewo...@googlegroups.com.
> To post to this group, send email to ninja-f...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ninja-framework/77e53b2a-a4df-4be1-83a9-e6d0d69c9025%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages