簡體   English   中英

firefox無法下載csv文件

[英]firefox cannot download csv file

我正在嘗試生成,然后默認使用 php 腳本下載 csv 文件。 我的代碼在 chrome 和 Internet Explorer 中運行良好,但相同的腳本在 Firefox 中不起作用。 在 Firefox 腳本中無法正確生成 csv 文件。 以下是腳本

require('core.php');
$master = new db();
$s = $master->getRecords();
function array2csv(array &$array)
{
   if (count($array) == 0) {
     return null;
   }
   ob_start();
   $df = fopen("php://output", 'w');
   fputcsv($df, array_keys(reset($array)));
   foreach ($array as $row) {
      fputcsv($df, $row);
   }
   fclose($df);
   return ob_get_clean();
}

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");
    header("Content-Disposition: attachment;filename={$filename}");
    header("Content-Transfer-Encoding: binary");
}
download_send_headers(site_title .' '. date("d M Y") . ".csv");
echo array2csv($s);
die();

HTML 和 JS

<a class="list-group-item" href="javascript:void(0);" onclick="return exportUser();">Export to CSV</a> 

function exportUser(){ 
        $.ajax({
        url: 'get_csv.php',
        type: 'POST',
        success: function() {
            window.location = 'get_csv.php';
        }
    });
}

我還附上了截圖以供理解。 在此處輸入圖片說明

嘗試添加此標題:

header('Content-Type: application/csv; charset=utf-8');

你需要在你的js函數中添加一個事件

函數 exportUser(event) 會做

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM