0

I've got a problem with a generated file. Here is my action:

<?php
public function exportAction()
{
    $this->checkSecurity('EDIT');

    $config = $this->getConfiguration();
    $resource = $this->findOr404();

    $style =  $this->render($config->getTemplate('export.css'), array(
        'resource' => $resource
    ));

    $response = new Response();
    $response->headers->set('Cache-Control', 'private');
    $response->headers->set('Content-type', 'plain/text');
    $response->headers->set('Content-type', 'application/octet-stream');
    $response->headers->set('Content-Disposition', 'attachment; filename="style-' . $resource->getSlug() . '.css";');
    $response->sendHeaders();
    $response->setContent($style);

    return $response;
}

My file is downloaded but in its content there is at the beginning:

HTTP/1.0 200 OK 
Cache-Control: no-cache 
Date:          Tue, 02 Sep 2014 09:16:34 GMT

Does anyone know why ?

Pierrick Aubin
  • 55
  • 2
  • 10

2 Answers2

0

i don t use a response as return for the generated file to be downloaded .

i simply fill a variable then display it as follow :

  $style =  $this->render($config->getTemplate('export.css'), array(
    'resource' => $resource
  ));


    header("Content-Type: text/calendar; filename='style-" . $resource->getSlug() . ".css'");
    header('Content-Length: '.strlen($style);
    header("Content-Transfer-Encoding: binary");
    header("Content-Disposition: inline; filename='style-" . $resource->getSlug() . ".css'"); 
    header("Expires: 0");
    header("Cache-Control: no-cache, must-revalidate");
    header("Pragma: no-cache");

 echo $style;
 exit;

i know this is not absolutely Object-side code but it works perfectly for generating thousand of ics file daily within one of our project

Charles-Antoine Fournel
  • 1,713
  • 1
  • 25
  • 37
  • Possible duplicate : http://stackoverflow.com/questions/13010411/symfony2-force-file-download?rq=1 – Charles-Antoine Fournel Sep 02 '14 at 12:04
  • I tried your code. The file isn't downloading. And I still have "HTTP/1.0 200 OK Cache-Control: no-cache Date: Tue, 02 Sep 2014 14:19:10 GMT" displayed on my screen before the css code I wanna download – Pierrick Aubin Sep 02 '14 at 14:20
  • I'va added the $response->sendHeaders(). Now the file is successfully downloaded but I still have "HTTP/1.0 200 OK Cache-Control: no-cache Date: Tue, 02 Sep 2014 14:22:02 GMT" at the beginning... – Pierrick Aubin Sep 02 '14 at 14:23
0

I finally found the solution. I forgot the getContent() on the render

<?php
public function exportAction()
{
    $this->checkSecurity('EDIT');

    $config = $this->getConfiguration();
    $resource = $this->findOr404();

    $style =  $this->render($config->getTemplate('export.css'), array(
        'resource' => $resource
    ));

    $response = new Response();
    $response->headers->set('Cache-Control', 'private');
    $response->headers->set('Content-type', 'plain/text');
    $response->headers->set('Content-type', 'application/octet-stream');
    $response->headers->set('Content-Disposition', 'attachment; filename="style-' . $resource->getSlug() . '.css";');
    $response->sendHeaders();
    $response->setContent($style->getContent());

    return $response;
}
Pierrick Aubin
  • 55
  • 2
  • 10