繁体   English   中英

导出并在php中以csv格式下载并不能在chrome和IE中工作,但在Firefox中工作

[英]export and download as csv in php not working in chrome and IE but working in firefox

我有一个简单的网页,该网页从数据库中提取一些数据并将其显示在html表中,并将数据存储在某些会话变量中。 我有一个“导出到csv”按钮,该按钮调用将结果导出到csv文件的页面。

ExportToCsv文件的代码为:

<?php
    session_start(); 
    include "conn/sqlserverConn.php";

    function download_send_headers($filename) {
    // disable caching
    $now = gmdate("D, d M Y H:i:s");
    header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
    header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
    header("Last-Modified: {$now} GMT");

    // force download  
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");

    // disposition / encoding on response body
    header("Content-Disposition: attachment;filename={$filename}");
    header("Content-Transfer-Encoding: binary");
    }

    $data = $_SESSION['data'];
    $rows = $_SESSION['row'];
    $header = $_SESSION['header'];

    download_send_headers("data_export_" . date("Y-m-d") . ".csv");
    $file = fopen("php://output", 'w');
    fputcsv($file,$header);


    for($i=0; $i<$rows; ++$i)
    {


        $valuesArray=array();
        foreach($data[$i] as $name => $value)
        {
            $valuesArray[]=$value;
        }
        fputcsv($file,$valuesArray);
    }
    fclose($file);


    die();

?>

我的代码在firefox中运行完美,但在chrome或IE中不运行。 在Chrome和IE上,显示错误404(找不到对象!)。 请告诉我是什么问题?

如博客文章中概述的: http : //www.exchangecore.com/blog/php-output-array-csv-headers/ ,我测试了以下内容,并在IE8-11和chrome版本34中起作用。假设它也可以在其他浏览器中正常工作。

使用的标题:

header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=' . $fileName);    

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM