& # 39;出口# 39;csv数据出现在我的Firefox控制台日志中,而不是导出的.csv文件(Yii 1.x)


'exported' csv data appears in my Firefox console log rather than an exported .csv file (Yii 1.x)

我有一个表单,一旦提交就从我的数据库中提取一些数据,我试图立即下载到csv文档中。

由于某些原因,数据都很好,在正确的csv格式中,但它只出现在我的Firefox浏览器的控制台日志中,而不是自动下载的文件,例如exported.csv

我有以下功能-我没有得到错误,因为一切都在工作,但由于某种原因,它不会自动下载csv ..我认为我的问题在outputCsv函数中。

任何想法?

public function actionSurveyStats()
{
    $id = Yii::app()->getRequest()->getQuery('id');
    $model = SurveyQuestionnaires::model()->findByPK($id);
    $this->bodyTitle = 'Your Survey';
    if (isset($_POST[get_class($model)])) {
        set_time_limit(0);
        ini_set('memory_limit', '786M');
        $filename = 'questionnaire_' .time(). '.csv';
        $completed->questionnaire_id = Yii::app()->request->getPost('questionnaire_id');
        $organisation_id = $_POST[get_class($model)]['organisation_id'];
        $this->outputCsv($model->getExportData($organisation_id), $filename);
    }
    $this->render('survey_stats',array(
        'model' => $model,
    ));
}

protected function outputCsv($rows, $filename = '') {
    if (empty($filename)) {
        $filename = (!is_null($this->model)) ? (strtolower(get_class($this->model)."_".$this->model->getPrimaryKey().".csv")) : "export.csv";
    }
    header('Content-Description: File Transfer');
    header("Content-Type: application/csv") ;
    header("Content-Disposition: attachment; filename=$filename");
    header("Pragma: no-cache");
    header("Cache-control: private");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    $fp = fopen('php://output', 'w');
    foreach($rows as $row) {
        fputcsv($fp, $row);
    }
    echo stream_get_contents($fp);
    fclose($fp);
    app()->end();
}

我的修复…是表单中的ajax…将此设置为false &它现在工作了!由于优

只需将'enableAjaxValidation'设置为false。

<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(
'id'                    => get_class($model),
'type'                  => 'horizontal',
'enableAjaxValidation'  => false,
'clientOptions'         => array(
    'validateOnSubmit'      => true,
    'validateOnChange'      => false,
    'validateOnType'        => false,
),
'htmlOptions'       => array(
    'class'                 => '',
    'autocomplete'          => 'off',
    'enctype'               => 'multipart/form-data'
),
)); ?>