Send a file header in controller (file download)

To return a downloadable file in a controller action, you have to send a file header in controller like that:

$fileName = 'mm_actioncodes_export_'.date("Ymd_His").'.csv';
$fp = fopen(Mage::getBaseDir('var') . DS .'export' . DS . $fileName, 'w');
fputcsv($fp, $codes, ';', '"');
fclose($fp);
$this->_prepareDownloadResponse($fileName, array(
    'type'  => 'filename',
    'value' => Mage::getBaseDir('var') . DS .'export' . DS . $fileName,
    //'rm'    => true // can delete file after use
));
return $this;

 

Leave a Reply

Your email address will not be published.

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.