【问题标题】:Firefox changes fileextension to ".csv.htm." when downloading a csv from myapp (php/phpexcel)Firefox 将文件扩展名更改为“.csv.htm”。从 myapp (php/phpexcel) 下载 csv 时
【发布时间】:2019-07-12 09:12:26
【问题描述】:

我的应用程序 (php/phpExcel) 提供了 .csv 文件的下载功能。当我使用 chrome 或 IE 下载文件时一切正常,但是当我使用 Firefox 进行下载时,它会将文件扩展名更改为“.csv.htm”,然后 Excel 会询问如何打开,因为扩展名和文件格式不适合。

header('Content-Encoding: UTF-8');
    header('Content-type: text/csv; charset=UTF-8');
    header('Content-Disposition: attachment;  filename="csvdownload_'.date("Y-m-d_H_i_s").'.csv');
    echo "\xEF\xBB\xBF"; // UTF-8 BOM

    // If you're serving to IE 9, then the following may be needed
    header('Cache-Control: max-age=1');

    // If you're serving to IE over SSL, then the following may be needed

    header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); 
    header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
    header ('Pragma: public'); // HTTP/1.0



    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
    $objWriter->setDelimiter(';');
    $objWriter->setEnclosure('"');
    $objWriter->setLineEnding("\r\n");

    $objWriter->save('php://output');

【问题讨论】:

    标签: php csv firefox phpexcel


    【解决方案1】:

    使用下面显示的标题,所有浏览器都应该很高兴并将文件下载为 CSV。如果您尚未计算字节数,只需将该标头省略即可。

    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Content-disposition: attachment; filename="Your_File_Name_Here.csv"');
    header('Content-length: ' . $FileBytes);
    header('Content-type: application/octetstream');
    header('Pragma: no-cache');
    header('Expires: 0');
    

    【讨论】:

      猜你喜欢
      • 2019-11-07
      • 2020-02-24
      • 2020-10-30
      • 1970-01-01
      • 2021-03-31
      • 1970-01-01
      • 2020-09-17
      • 2015-05-06
      • 1970-01-01
      相关资源
      最近更新 更多