Symfony2 – Download Filestream as StreamedResponse

By | 18/04/2014

Sometimes you just get some content in form of a byte stream and you want to make a file out of it and download it.

Here is a short tutorial on how to do this with a StreamedResponse in Symfony2 and how you can write a functional test to validate the basics for the action.

Step 1 would be to write the test:

Now all the tests should be very red, so it is the right time implementing the controller. I don’t show how to set up the route for that, just how the controller looks like.

At the place where I call $report->getFileContent you can also use a buffer to read for example a large file.