Dynamic content for download (Content-Disposition the laravel way...)
Hi,
I am working on a CSV download feature for a project. I finally got it working with the following (relies directly on Symfony Components (http://symfony.com/doc/current/components/http_foundation.html#serving-files)). When I tried replacing $response = new Response($fileContent); with $response = response() it didn't work. Without, that it works fine within a controller method. I'm trying to make/keep it as Laravel as possible without requiring to import Symfony components directly.
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
$fileContent = ...; // the generated file content
$response = new Response($fileContent);
$disposition = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
'foo.pdf'
);
return $response->headers->set('Content-Disposition', $disposition);
@pmall Good catch. I changed the title. Basically, wanted to confirm if there was any way to directly use laravel's Response (via helper response()) to achieve it. I understand, Laravel uses HttpFoundation.
both work for me. I'm sure I can reduce it to 1 line or even create a quick shortcut method somewhere. I like using helpers like response() as I feel they work for more versions to come :)