Reputation: 305
How to implement similar solution as this:
$file = generateCSV();
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename="asd");
header("Pragma: no-cache");
header("Expires: 0");
echo $file;
exit();
using Symfony Response object?
Right now I have
$file = generateCSV();
$response = new Response($file);
$response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'asd');
$response->headers->set('Content-Disposition', 'text/csv');
$response->headers->set('Pragma', 'no-chache');
$response->headers->set('Expires', '0');
return $response;
but it doesn't seem to work because instead of sending file to download it renders my file as text in browser.
Any help will be appreciated.
Upvotes: 1
Views: 2067
Reputation: 1209
I have done this like this :
$response = new StreamedResponse();
$response->setCallback(
/* here you generate your csv file */
);
$response->setStatusCode(200);
$response->headers->set('Content-Type', 'text/csv; charset=utf-8');
$response->headers->set(
'Content-Disposition',
'attachment; filename="file.csv"'
);
return $response;
Upvotes: 1
Reputation: 103
You did not specify content type. My download example:
$dispositionHeader = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
$filename . '.csv'
);
$response->headers->set('Content-Type', 'text/csv; charset=utf-8');
$response->headers->set('Pragma', 'public');
$response->headers->set('Cache-Control', 'maxage=1');
$response->headers->set('Content-Disposition', $dispositionHeader);
Upvotes: 2