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..
asked Apr 13, 2013 at 11:40
Manoj Sreekumar
7483 gold badges15 silver badges32 bronze badges
-
the problem will probably be file system access no matter if you directly try saving from Javascript as CSV or pass the data to the server and return a CSV file to be saved on the client.frequent– frequent2013年04月13日 13:44:43 +00:00Commented Apr 13, 2013 at 13:44
1 Answer 1
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);
}
answered Apr 13, 2013 at 12:02
What have you tried
11.2k4 gold badges35 silver badges45 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default