1

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..

1
  • 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. Commented Apr 13, 2013 at 13:44

1 Answer 1

2

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.