title: How to force download a .csv file in Symfony 2, using Response object?
body: <p>I'm making a "Download" controller using Symfony 2, that has the sole purpose of sending headers so that I can force a .csv file download, but it isn't working properly.</p>
  
  <pre><code>$response = new Response();
  $response-&gt;headers-&gt;set('Content-Type', "text/csv");
  $response-&gt;headers-&gt;set('Content-Disposition', 'attachment; filename="'.$fileName.'"');
  $response-&gt;headers-&gt;set('Pragma', "no-cache");
  $response-&gt;headers-&gt;set('Expires', "0");
  $response-&gt;headers-&gt;set('Content-Transfer-Encoding', "binary");
  $response-&gt;headers-&gt;set('Content-Length', filesize($fileName));
  $response-&gt;prepare();
  $response-&gt;sendHeaders();
  $response-&gt;setContent(readfile($fileName));
  $response-&gt;sendContent();
  </code></pre>
  
  <p><code>$fileName</code> is a <code>"info.csv"</code> string. Such are my actions inside my controller, there's no return statement. When I tried returning the <code>Response</code> Object, the contents of the file were displayed in the browser, not my intended result.</p>
  
  <p>The problem I've found is that in some pages I do get my <code>info.csv file</code>, but in anothers  all I get is a message: </p>
  
  <p><code>No webpage was found for the web address: http://mywebpage.com/download</code>
  <code>Error 6 (net::ERR_FILE_NOT_FOUND): The file or directory could not be found.</code></p>
  
  <p>I'm completely sure the file exists, so there must be another thing wrong. Also, routing.yml is working correctly, since I do get the file from other pages that also link to that path.
  The Apache error log doesn't show anything about it.</p>
  
  <p>Has anyone forced the download of a .csv file on Symfony 2 before? If so, what am I doing wrong?</p>