Reputation: 748
I have a javascript array which i have to save as CSV in client environment..Is that possible using jquery? I have to make in work in all major browsers too..I'm using symfony2 framework..How can this be done if I can pass this array to the server..Please explain the best possible way to pass array as I'm having a pretty large array of associativ arrays..
Upvotes: 1
Views: 962
Reputation: 11148
You can send the data to the server however you'd like (using $_POST
, AJAX
, etc.). Once the data arrives at the server, though, this is how I would go about sending the data to a CSV file.
$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);
}
Upvotes: 2