15

I'm trying to export a database table as a .csv downloadable from the browser. My code is zend framework based and I'm almost there with the following action:

public function exportTableAction()
{
    $this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();

    $fileName = $this->_getParam('fileName');
    $tableName = $this->_getParam('tableName');       

    header('Content-type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.$fileName.'"');

    echo $this->getCsv($tableName, $fileName);
}

I can download my .csv file containing valid data. However, even if I disabled the layout and the renderer, I still get the output of the header, sidebar, and footer of my page at the end of my .csv file. Is there a way to disable any html output other than the one generated in my exportTableAction? Or can I send the header information and the csv string to the browser in a different way?

BTW: I'm using the action stack plugin to help me render the header and sidebar as follows:

...
$actionStack = $front->getPlugin('Zend_Controller_Plugin_ActionStack');
$actionStack->pushStack($userlogAction);
$actionStack->pushStack($rightcolAction);

Cheers, Adrian

4
  • Are you using the ActionStack plugin for your header, sidebar, and footer? Commented Jul 16, 2009 at 10:33
  • Yes, indeed. I have the following line in my bootstrap.php: $frontController->registerPlugin( new Project_Controller_Plugin_ActionSetup()); Can I disable this in my exportTableAction? Commented Jul 16, 2009 at 11:06
  • 1
    hey - any chance you could post the implementation of your getCsv($tableName, $fileName); method? Commented Dec 28, 2009 at 12:51
  • Hi!! Can you please share the code of getCSV()? I need exacly this and It would be appreciated. Thanks! Commented Nov 21, 2012 at 11:14

4 Answers 4

7

We found a solution to the problem. I replaced the following line

$this->_helper->viewRenderer->setNoRender();

by

$this->_helper->viewRenderer->setNeverRender();

If setNeverRender() is used, no views are rendered (from plugin neither).

Sign up to request clarification or add additional context in comments.

Comments

6

You can use contextSwitch action helper.

public $contexts = array(
    'test'     => array('csv')
);

public function testAction()
{
    $filename = time() . '.csv';
    $this->_helper->contextSwitch()->addContext('csv',
            array('suffix' => 'csv',
                  'headers' => array('Content-Type' => 'application/csv',
                                     'Content-Disposition' => 'attachment; filename="'. $filename.'"')))->initContext('csv');
    ........................
    ........................
}

Comments

1
$this->_helper->viewRenderer->setNoRender();
$this->view->layout()->disableLayout();

$response = $this->getResponse();
$response->setHeader('Content-type', 'application/octet-stream');
$response->setHeader('Content-Disposition', 'attachment; filename="contatos.csv"');

echo $your_csv_content;

Comments

0
public function getCsv($tableName, $fileName)
{   
    $content = new $tableName();

    $content_arr = array();     
    $content_arr = $content->fetchAll('1=1','id ASC');

    $csv_terminated = "\n";
    $csv_separator = ",";
    $csv_enclosed = '"';
    $csv_escaped = "\\";        

    $schema_insert = "";

    $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
    stripslashes("Email Address")) . $csv_enclosed;
    $schema_insert .= $l;
    $schema_insert .= $csv_separator;

    $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
    stripslashes("Add Date")) . $csv_enclosed;
    $schema_insert .= $l;
    $schema_insert .= $csv_separator;

    $out = trim(substr($schema_insert, 0, -1));
    $out .= $csv_terminated;

    if(count($content_arr) > 0)
    {               
        foreach($content_arr as $content)
        {   
             $schema_insert = '';

            if ($content->email != '' || $content->add_date != '')
            {                           
                    $schema_insert .= $csv_enclosed . 
                    str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $content->email) . $csv_enclosed;

                     $schema_insert .= $csv_separator;

                    $schema_insert .= $csv_enclosed . 
                    str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $content->add_date) . $csv_enclosed;
                    $schema_insert .= $csv_separator;

           }        

            $schema_insert .= $csv_separator;
            $out .= $schema_insert;
            $out .= $csv_terminated;

        }
    }
    return $out;
}`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.