无法使用PHP和MySQL在浏览器上下载CSV文件

我需要将mysql记录下载到浏览器上的csv文件中,但是根据我的代码,它无法正常工作。这是我的代码:

$scope.downloadAllCustomerData=function(){
        var url2='../service/admin/customer/customer.php?action=downloadAllCustomerData';
        var method='GET';
        var data1='';
        DataService.connectToServerSideScript(method,url2,data1)
        .then(function(response) {
            console.log('response',response);

        },function(error) {

        })
    }

这是我的客户端代码,它要求分别下载.CSV文件。

if ($_REQUEST["action"]=="downloadAllCustomerData") {
        header('Content-Type: text/csv; charset=utf-8');  
        header('Content-Disposition: attachment; filename=data.csv');  
        $output = fopen("php://output","w");  
        fputcsv($output,array('ID','Name','Email','MObile No'));  
        $sql = "SELECT user_id,name,email,mobile from cb_customer_info ORDER BY user_id DESC";
        $stmt = $db->prepare($sql);
        if ($stmt->execute()) {
            $row = $stmt->fetchAll();
            $number_of_rows= count($row);
            if ($number_of_rows > 0) {
                fputcsv($output,$row);
                $data=array("status"=>1,"msg"=>"Downloaded all data successfully.");
            }else{
                $data=array("status"=>0,"msg"=>"No record found.");
            }
            fclose($output);
        }
        echo json_encode($data);
    }

在这里,我正在尝试将mysql记录下载到.csv文件中。我需要用户何时单击按钮,这些记录将下载到浏览器中。

longxin0118 回答:无法使用PHP和MySQL在浏览器上下载CSV文件

更好地形成数据并回显/打印到浏览器,而不是使用php:// output stream。

用于下载的常用标题,

Properties > Build > Platform Target > 'x86'
本文链接:https://www.f2er.com/3161624.html

大家都在问