Firefox无法下载CSV文件 [英] firefox cannot download csv file

查看:224
本文介绍了Firefox无法下载CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图生成,然后默认使用php脚本下载csv文件。我有在铬和Internet Explorer工作正常的代码,但相同的脚本不能在Firefox中工作。在Firefox脚本不会正确生成CSV文件。以下是脚本

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


函数download_send_headers($ filename){

//禁用缓存
$ now = gmdate(D,d MYH:i:s );
header(Expires:Tue,2001年七月3日06:00:00);
header(Cache-Control:max-age = 0,no-cache,must-revalidate,proxy-revalidate);
header(Last-Modified:{$ now} GMT);
$ b $ //强制下载
header(Content-Type:application / force-download);
header(Content-Type:application / octet-stream);
header(Content-Type:application / download);


//对响应体的处理/编码
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-itemhref =javascript:void(0); onclick =return exportUser();>导出为CSV< / a> 

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

我还附上了一张屏幕截图以供了解。

解决方案

您需要为您的js函数添加一个事件

函数exportUser(event)会执行它

I'm trying to generate and then by default download csv file using php script. I have code which is working fine in chrome and internet explorer but the same script does not works in firefox. In firefox script does not generating csv file properly. Following are the script

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 and 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';
        }
    });
}

I also attached a screenshot for understanding.

解决方案

You need to add an event to your js function

function exportUser(event) will do it

这篇关于Firefox无法下载CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆