简体   繁体   中英

Download CSV file from server to local direcory

I have written a script in which I create and save a CSV file in a directory in my server. The CSV file contains attributes from a query. The whole process is executed on a button click using Jquery and Ajax.

What I want to do now is to be able to save locally the generated CSV. I have found similar questions but I haven't found an answer on how to create a dialog box which will prompt the client to specify the location to save the file.

This is part of my Jquery code:

var nodeId = 'something';
var ajaxurl = 'requestsII/exportNodeData.php', // script to run
        data =  {nodeId:nodeId}; // data to pass
        $.post(ajaxurl, data, function (response) {     
                //alert(response);

        }); 

And this is my PHP script:

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");  // EDITED

if ($db) {
    $stmt = $db->prepare('SELECT kodpop AS "ΚΩΔΙΚΟΣ ΚΟΜΒΟΥ",itemcode AS "ΚΩΔΙΚΟΣ ΕΞΟΠΛΙΣΜΟΥ",temaxia AS "ΤΕΜΑΧΙΑ",status AS "STATUS" FROM bom WHERE type IN (:typeI, :typeII) AND kodpop = :nodeId');
    $stmt->execute(array('typeI' =>$typeI,'typeII' => $typeII,'nodeId' => $nodeId));

    #$results=$stmt->fetchAll(PDO::FETCH_OBJ);
    #$json=json_encode($results);
    #echo($json);

    $filename = '/var/www/dkar/ruralBroadband/ruralApp/rural/csvExports/'.$nodeId.'.csv';

    $data = fopen($filename, 'w');

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        fputcsv($data, $row);
    }

    fclose($data);

}

echo($data); // EDITED

The question is how can I download locally the CSV file I create inside the PHP script?

Try this code

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");

echo "record1,record2,record3\n";

Edit :

Try This code

if ($db) 
{
    $stmt = $db->prepare('SELECT kodpop AS "ΚΩΔΙΚΟΣ ΚΟΜΒΟΥ",itemcode AS     "ΚΩΔΙΚΟΣ ΕΞΟΠΛΙΣΜΟΥ",temaxia AS "ΤΕΜΑΧΙΑ",status AS "STATUS" FROM bom WHERE type  IN (:typeI, :typeII) AND kodpop = :nodeId');
$stmt->execute(array('typeI' =>$typeI,'typeII' => $typeII,'nodeId' => $nodeId));

#$results=$stmt->fetchAll(PDO::FETCH_OBJ);
#$json=json_encode($results);
#echo($json);

$filename = '/var/www/dkar/ruralBroadband/ruralApp/rural/csvExports/'.$nodeId.'.csv';



header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=".$filename);  


while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    print_r($row);
}
}

After all I didn't use the above method and I did that, which works fine and its more straight forward:

var ajaxurl = 'exportNodeData.php', // script to run
                data =  {nodeId:nodeId}; // data to pass
                $.post(ajaxurl, data, function (response) {     
                    document.location.href = 'https://www.ruralauditor.gr/csvExports/'+nodeId+'.zip';

                }); 

This is the ajax request, which I execute in order to create the CSV file (which is also zipped). I then use this line:

document.location.href = 'https://www.ruralauditor.gr/csvExports/'+nodeId+'.zip';

in order to force the download of the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM