From 3159aac006db9d01f38ac495b92e1adbb6fbd95f Mon Sep 17 00:00:00 2001 From: Sergio N Date: Fri, 9 Feb 2018 15:58:41 -0300 Subject: [PATCH] Add Donwload direct file CSV Add Donwload direct file CSV --- Controller/Component/CsvComponent.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Controller/Component/CsvComponent.php b/Controller/Component/CsvComponent.php index 1513b60..09f4ec6 100644 --- a/Controller/Component/CsvComponent.php +++ b/Controller/Component/CsvComponent.php @@ -100,6 +100,11 @@ public function import($filename, $fields = array(), $options = array()) { */ public function export($filename, $data, $options = array()) { $options = array_merge($this->defaults, $options); + + if($filename === null){ + $filename = "php://output"; + $this->sendHttpHeaders(); + } // open the file if ($file = @fopen($filename, 'w')) { @@ -141,5 +146,26 @@ public function export($filename, $data, $options = array()) { return false; } } + + public function setFileNameOut($name = 'export-data.csv') + { + return $this->filename_out = $name; + } + + public function getFileNameOut() + { + return $this->filename_out; + } + + public function sendHttpHeaders() + { + header('Content-Encoding: UTF-8'); + header('Content-Type: text/csv; charset=utf-8' ); + header(sprintf( 'Content-Disposition: attachment; filename=%s', basename($this->getFileNameOut()) ) ); + header('Content-Transfer-Encoding: binary'); + header('Expires: 0'); + header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); + header('Pragma: public'); + } } ?>