您可以随心所欲地将数据发送到服务器(使用
$_POST
,
AJAX
等)。不过,一旦数据到达服务器,这就是方法
我
将数据发送到CSV文件。
$serialized_data = $_POST['some_data_array'];
if($downloadFile) // simple condition
{
$fp = fopen('php://memory', 'w+'); // open up write to memory
foreach($serialized_data as $row) // $serialized_data represents what you sent to the server from JS
{
fputcsv($fp, $row);
}
rewind($fp);
$csvFile = stream_get_contents($fp);
fclose($fp);
header('Content-Type: text/csv');
header('Content-Length: '.strlen($csvFile));
header('Content-Disposition: attachment; filename="yourFile.csv"');
exit($csvFile);
}