dongpao1905 2013-03-03 07:21
浏览 49
已采纳

下载的.csv文件包含HTML标记作为文本

I want to download a .csv file through link.For that a Download link is defined in a template file.

To generate .csv file I have written a piece of code as follows.

public function loadPartnerApplicantData() {
    $inboundBo = BoFactory::getInboundHttpRequestBo();
    $fileType = $inboundBo->getSanitizedGetParam('f');
    $formId = $inboundBo->getSanitizedGetParam('fid');
    ServiceFactory::getFormService()->loadFormDetails($formId);
    $dbTable = BoFactory::getFormBo()->getFormDbTable($formId);
    $formName = slugify(BoFactory::getFormBo()->getFormName());
    $fileName = $formName . "." . time();
    $fieldMasterSqlQuery = "SELECT field_name,field_label FROM" . FORM_FIELDS_MASTER_v2 . "where form_id='$formId' order by serial_no";
    $fieldMasterSqlQueryStatus = mysql_query(mysql_fetch_assoc($fieldMasterSqlQuery));
    $csvHeader = "";
    $fieldNameArray = array();
    foreach ($fieldMasterSqlQueryStatus as $key => $value) {
        if ($value['field_name'] == 'declaration' || $value['field_name'] == 'docPicture') {
            continue;
        }
        $csvHeader.= "\"{$value['field_label']}\";";
        $fieldNameArray[] = $value['field_name'];
    }
    $queryString = implode(",", $fieldNameArray);
    $dbTableSqlQuery = "SELECT $queryString FROM `$dbTable`";
    $dbTableSqlQueryStaus = mysql_query(mysql_fetch_assoc($dbTableSqlQuery));
    ef_clearBuffer();
   // To generate csv
    header("Content-type: text/csv");
    header("Content-Disposition: attachment; filename=$fileName.csv");
    header("Pragma: no-cache");
    header("Expires: 0");
    echo($csvHeader);
    foreach ($dbTableSqlQueryStaus as $applicantData) {
        echo "
";
        foreach ($fieldNameArray as $fieldName) {
            echo "\"$applicantData[$fieldName]\";";
        }
        echo "
";
    }
}

And the required .csv is generated .

But at the end of .csv file HTML tags of the browser is getting displayed. which should not be there.

Please suggest me to remove the html content from the generated .csv file.

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • dougang5993 2013-03-31 03:24
    关注

    Since your function handles the request till the end (i.e., delivers all data), and you don't want the framework (whichever you're using) to continue processing, add

    exit(0);
    

    as last line of your function. That will halt the processing after the content is delivered and prevent the framework/environment from sending additional data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?