php 浏览器直接下载csv问题

php浏览器直接下载csv问题,浏览器无法下载,我是通过ajax发送请求到后端。然后后端执行csv导出。

public function _export(&$data, $title_arr, $file_name = '') {
        $csv_data = '';
        /** 标题 */
        $nums = count($title_arr);
        for ($i = 0; $i < $nums - 1; ++$i) {
            $csv_data .= '"' . $title_arr[$i] . '",';
        }
        if ($nums > 0) {
         $csv_data .= '"' . $title_arr[$nums - 1] . "\"\r\n";
        }
        foreach ($data as $k => $row) {
            for ($i = 0; $i < $nums - 1; ++$i) {
                $row[$i] = str_replace("\"", "\"\"", $row[$i]);
                $csv_data .= '"' . $row[$i] . '",';
            }
            $csv_data .= '"' . $row[$nums - 1] . "\"\r\n";
            unset($data[$k]);
        }

        $csv_data = mb_convert_encoding($csv_data, "GBK", "UTF-8");
        // $file_name = empty($file_name) ? date('Y-m-d-H-i-s', time()) : $file_name;
        // if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE")) { // 解决IE浏览器输出中文名乱码的bug
        //  $file_name = urlencode($file_name);
        //  $file_name = str_replace('+', '%20', $file_name);
        // }
        // echo $csv_data;
        // echo $file_name;
        // exit;

        Header("Content-type: application/octet-stream;charset=utf-8");
        Header("Content-Disposition:attachment;filename=" . $file_name);
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        Header('Expires:0');
        Header('Pragma:public');
        echo $csv_data;
    }

求助

阅读 7.9k
5 个回答

ajax不可以下载文件.ajax的返回值类型是json,text,html,xml类型,或者可以说ajax的发送,接受都只能是string字符串,不能流类型,所以无法实现文件下载。
正确做法是 window.open('下载链接')

ajax本来就没法下载, location.href=跳转.

1.我建议生成CVS 文件和下载文件分开,不然看起来很乱,&符号不要乱用
2.下载文件建议使用readfile
if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.basename($file).'"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);
    exit;
}

生成到服务器,直接连接过去不是很方便吗?

            ob_end_clean();
            header('Content-Type: application/vnd.ms-excel');
            header("Content-Disposition: attachment;filename='{$filename}.xls'");
            header('Cache-Control: max-age=0');
            // 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 ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
            header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
            header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
            header ('Pragma: public'); // HTTP/1.0
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
            $objWriter->save('php://output');
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题