title: How can I export a CSV using Symfony's StreamedResponse?
body: <p>My code looks fine, I get status 200, I get the right headers, ... and yet my CSV file created will not donwload...</p>
  <p>There is no error, so I do not understand why it's failing.</p>
  <p>Here is my code:</p>
  <pre><code>namespace Rac\CaraBundle\Manager;
  
  /* Imports */
  use Symfony\Component\PropertyAccess\PropertyAccess;
  use Symfony\Component\Validator\ValidatorInterface;
  use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  use Doctrine\Common\Persistence\ObjectManager;
  use Symfony\Component\HttpFoundation\StreamedResponse;
  
  /* Local Imports */
  use Rac\CaraBundle\Entity\Contact;
  
  /**
   * Class CSV Contact Importer
   */
  class CSVContactImporterManager {
  
      /**
       * @var ObjectManager
       */
      private $om;
  
      /**
       * @var EventDispatcherInterface
       */
      private $eventDispatcher;
  
      /**
       * @var ValidatorInterface
       */
      private $validator;
  
      /**
       * @var ContactManager
       */
      private $contactManager;
  
  
      /**
       * @param EventDispatcherInterface $eventDispatcher
       * @param ObjectManager            $om
       * @param Contact                  $contactManager
       *
       */
      public function __construct(
      EventDispatcherInterface $eventDispatcher, ObjectManager $om, ValidatorInterface $validator, ContactManager $contactManager
      ) {
          $this-&gt;eventDispatcher = $eventDispatcher;
          $this-&gt;om = $om;
          $this-&gt;validator = $validator;
          $this-&gt;contactManager = $contactManager;
      }
      public function getExportToCSVResponse() {
          // get the service container to pass to the closure
          $contactList = $this-&gt;contactManager-&gt;findAll();
          $response = new StreamedResponse();
          $response-&gt;setCallback(
              function () use ($contactList) {
              //Import all contacts
              $handle = fopen('php://output', 'r+');
              // Add a row with the names of the columns for the CSV file
              fputcsv($handle, array('Nom', 'Prénom', 'Société', 'Position', 'Email', 'Adresse', 'Téléphone', 'Téléphone mobile'), &quot;\t&quot;);
              $header = array();
              //print_r($contactList);
              foreach ($contactList as $row) {
                  fputcsv($handle, array(
                      $row-&gt;getFirstName(),
                      $row-&gt;getLastName(),
                      $row-&gt;getCompany(),
                      $row-&gt;getPosition(),
                      $row-&gt;getEmail(),
                      $row-&gt;getAddress(),
                      $row-&gt;getPhone(),
                      $row-&gt;getMobile(),
                      ), &quot;\t&quot;);
              }
              fclose($handle);
          }
          );
          $response-&gt;headers-&gt;set('Content-Type', 'application/force-download');
          $response-&gt;headers-&gt;set('Content-Disposition', 'attachment; filename=&quot;export.csv&quot;');
  
          return $response;
      }
  </code></pre>
  <p>And my controller :</p>
  <pre><code>    use Rac\CaraBundle\Entity\Contact;
      use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
      use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
      use Symfony\Component\HttpFoundation\Request;
      use UCS\Bundle\RichUIBundle\Controller\BaseController;
      use UCS\Bundle\RichUIBundle\Serializer\AbstractListSerializer;
      
      /**
       * Contact BackOffice Environment Controller.
       *
       *
       *
       * @Route(&quot;/contact_environment&quot;)
       */
      class ContactEnvironmentController extends BaseController{
          /* My code here..*/
      
      
         /**
           * @Route(&quot;/export&quot;, name=&quot;contact_environment_export&quot;,options={&quot;expose&quot;=true})
           * @Method(&quot;GET&quot;)
           *
           * @return type
           */
          public function exort(){
              $manager = $this-&gt;get(&quot;cara.csv_contact_importer_manager&quot;);
             return $manager-&gt;getExportToCSVResponse();
      
          
  
  }
  }
  </code></pre>
  <p>My response headers:</p>
  <pre><code>Cache-Control:no-cache, private
  Connection:close
  Content-Disposition:attachment; filename=&quot;export.csv&quot;
  Content-Type:application/force-download
  </code></pre>