How can one download CSV files through the browsers directly?

I am trying to allow my user click on a link on the application which will open open up a dialog box that gives the user as choice to save the csv file. How do I accomplish this?

thanks

I don't think that's

I don't think that's possible in case if I understand you correct. Browser build a list of "Save As..." elements based on operation system settings. But at least you could try to play around with Content-type server response header. In my opinion something like "Content-type: text/csv" could help in case if your browser able to process this kind of response.

This is my code: Please tell me what I am doing wrong

Basically I want a dialog box to appear on the users browser that gives the user a choice to save the file to their computer

<? php
                header('Expires: 0');
                header('Cache-control: private');
                header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                header('Content-Description: File Transfer');
                header('Content-Type: application/csv');
                header('Content-disposition: attachment; filename=file_name.csv');
                // Connection is openned to oracle database
                //$connection = OCILogon("srts", "srts", "6.82.5.68:1721/infrt.ghes.net");
                $connection = OCILogon("smarts", "smarts", "ac5-oma.ghes.net:1521/infrp.ghes.net");
                if (!$connection)
                {
                        echo "Unable to connect: " . var_dump(OCIError());
                        die();
                }

                $statement = OCIParse($connection, "SELECT * FROM SMARTS.TRANSPONDERLOADINGDATA ORDER BY CUSTOMERNAME");
                $condition = OCIExecute($statement, OCI_DEFAULT);
                $nrows = OCIFetchstatement($statement, $results);
                if ($nrows >
0)
                {
                        $out = '';

                        $fields = array("ID","CUSTOMERNAME","NUMBEROFSITES", "SATELLITE", "TRANSPONDER", "SERVICEPLAN", "PRODUCTTYPE", "NOTES");
                        $columns = 8 - 1;

                        for($i = 0; $i < $columns; $i++)
                        {
                                $l = $fields[$i];
                                $out .= $l.',';
                        }
                        $out .= $fields[7];
                        $out .= "\n";


                        if ($condition)
                        {      
                        print $out;
                        }
                }

                elseif ($nrows == 0)
                {
                        echo "Sorry there are no customers to import csv file at this time.";
                }

                OCIFreeStatement($statement);

                //log off
                OCILogoff($connection);
        ?>

What am I doing wrong?

Well I see, this is much

Well I see, this is much better.
First of all you need to send out this header:

header('Content-Disposition: attachment; filename="foo.csv"');

It will notice the browser that you need to show download dialog.

Second one is to play around content-type. For example you could try this:

header("Content-Type: text/csv");

Let me know if it works or doesn't.

Its still not working: Please check out my code to see my errors

First of all the rest of my code works well, I am just having issues with the csv file downloading issue. The php code in question is called by this script below which is the main html file (take note of this line that says "Download CSV file":

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:b="http://www.backbase.com/b" xmlns:s="http://www.backbase.com/s">
        <head>
                <title>Ajax-Enabled Transponder Statistics Tracker </title>
                <link rel="stylesheet" href="transponderData/form.css" type="text/css" media="all"/>
                <script type="text/javascript" src=" ../Backbase/3_3/bpc/boot.js" ></script>
                <script type="text/javascript">
                        //Enable beta Opera support
                        bpc.enableOpera = true;
                </script>      
        </head>
        <body onload="bpc.boot('../Backbase/3_3/');" b:controlpath="../Backbase/3_3/controls/backbase/">
                <xmp b:backbase="true" style="display:none;">
                        <s:execute>
                                <s:task b:action="show" />
                        </s:execute>
                        <!--
                        <s:include b:url="transponderData/forms.xml"/>

                        -->


                                        <b:panelset b:cols="16em *" >
                                                <b:panel id="panel1" class="b-step-overview b-panel" style="color:#999999;">
                                                        <a class="b-step-overview b-panel" style="color:#999999;" b:action="load" b:url="transponderData/AddNewCustomer.xml" b:destination="id('panel2')" b:mode="replacechildren"><b>Add Customer</b></a><br/><br/>
                                                        <a class="b-step-overview b-panel" style="color:#999999;" b:action="load" b:url="transponderData/ChooseSummaryType.xml" b:destination="id('panel2')" b:mode="replacechildren"><b>View Summary</b></a><br/><br/>
                                                        <a class="b-step-overview b-panel" style="color:#999999;" b:action="load" b:url="transponderData/UpdateCustomerInfo.xml" b:destination="id('panel2')" b:mode="replacechildren"><b>Update Customer</b></a><br/><br/>
                                                        <a class="b-step-overview b-panel" style="color:#999999;" b:action="load" b:url="transponderData/Query.xml" b:destination="id('panel2')" b:mode="replacechildren"><b>Calculate % loading</b></a><br/><br/>             
                                                        <a class="b-step-overview b-panel" style="color:#999999;" b:action="load" b:url="transponderData/Delete.xml" b:destination="id('panel2')" b:mode="replacechildren"><b>Delete Customer</b></a><br/><br/>
                                                        <a class="b-step-overview b-panel" style="color:#999999;" b:action="load" b:url="transponderData/csv.php" b:destination="id('panel2')" b:mode="replacechildren"><b>Download CSV file</b></a><br/><br/>                                                                         
                                                </b:panel>
                                                <b:panel id="panel2" class="b-panel" style="overflow: auto;"></b:panel>
                                        </b:panelset>

                </xmp>
        </body>
</html>

Here is the php code I am having issues with. I tried using your instructions but no dialog box opens, instead it prints to screen.

Below is the php code:

<?php header('Content-Disposition: attachment; filename="foo.csv"');
header("Content-Type: text/csv");
?>

<div xmlns="http://www.w3.org/1999/xhtml" xmlns:b="http://www.backbase.com/b" xmlns:s="http://www.backbase.com/s">
<?php
                // Connection is openned to oracle database
                //$connection = OCILogon("arts", "arts", "6.2.4.1:1521/adbt.ghes.net");
                $connection = OCILogon("arts", "arts", "rameta.ghes.net:1521/adbp.ghes.net");
                if (!$connection)
                {
                        echo "Unable to connect: " . var_dump(OCIError());
                        die();
                }

                $statement = OCIParse($connection, "SELECT * FROM SMARTS.TRANSPONDERLOADINGDATA ORDER BY CUSTOMERNAME");
                $condition = OCIExecute($statement, OCI_DEFAULT);
                $nrows = OCIFetchstatement($statement, $results);
                if ($nrows >
0)
                {
                        $out = '';

                        $fields = array("ID","CUSTOMERNAME","NUMBEROFSITES", "SATELLITE", "TRANSPONDER", "SERVICEPLAN", "PRODUCTTYPE", "NOTES");
                        $columns = 8 - 1;

                        for($i = 0; $i < $columns; $i++)
                        {
                                $l = $fields[$i];
                                $out .= $l.',';
                        }
                        $out .= $fields[7];
                        $out .= "\n";



                        if ($condition)
                        {      
                        print $out;
                                //exit();
                        }
                }

                elseif ($nrows == 0)
                {
                        echo "Sorry there are no customers to import csv file at this time.";
                }

                OCIFreeStatement($statement);

                //log off
                OCILogoff($connection);
        ?>

</div>

I really do need your help, thank

As I mentioned before,

As I mentioned before, nothing is wrong with your current implementation. For example this code works fine for me:

<?php
        header('Content-Disposition: attachment; filename="foo.csv"');
        header("Content-Type: text/csv");
        echo 'your csv data is going to be here...';
?>

In Firefox download dialog appears on screen and I can choose the location where I want to save this file.
But this behavior depends on the browser, because if browser are able to show up the content, there is now way to overwrite this. What you can do is, change "Content-type" header, and use something "UNKNOWN", then browser will decide to not print this, but will show save dialog. However there is no guarantee that it will work as you expect, at least you can try this:
<?php
        //header('Content-Disposition: attachment; filename="foo.csv"');
        header("Content-type: application/x-unknown-file");
        echo 'your csv data is going to be here...';
?>

I think it should help.

This is the error message I am getting now ...

Mind you my code remains the same as the last time I did not change anything. What do you think the problem might be?

Message:       
1. [11:03:16.317] CRITICAL: Error parsing:

XML Parsing Error: syntax error
Location: <a href="http://aapp-01.ghes.net:777/transponderstats/transponderStat.html
Line"
title="http://aapp-01.ghes.net:777/transponderstats/transponderStat.html
Line"
>
http://aapp-01.ghes.net:777/transponderstats/transponderStat.html
Line</a> Number 1, Column 1:
Text:
your csv data is going to be here...<div xmlns="http://www.w3.org/1999/xhtml" xmlns:b="http://www.backbase.com/b" xmlns:s="http://www.backbase.com/s">
^

Please help

thanks

Its seems the HTML file is expecting a "div" and when I take that out I also get a XML syntax error. I am really confused

It seems that you use non -

It seems that you use non - valid xml.

where exactly is the XML wrong

I cannot figure it out. If I take out the "div" I get an error and if I leave it the data gets printed to the screen

Thanks and I look forward to a solution

My code is still not working

It displays the content to the screen. I am very frustrated with it. What else can I do?

<?php header('Content-Disposition: attachment; filename="foo.csv"');
header("Content-Type: text/csv");
?>

<div xmlns="http://www.w3.org/1999/xhtml" xmlns:b="http://www.backbase.com/b" xmlns:s="http://www.backbase.com/s">
<?php
                // Connection is openned to oracle database
                //$connection = OCILogon("arts", "arts", "6.2.4.8:11/adbt.ghes.net");
                $connection = OCILogon("arts", "arts", "ameta.ghes.net:1521/adbp.ghes.net");
                if (!$connection)
                {
                        echo "Unable to connect: " . var_dump(OCIError());
                        die();
                }

                $statement = OCIParse($connection, "SELECT * FROM SMARTS.TRANSPONDERLOADINGDATA ORDER BY CUSTOMERNAME");
                $condition = OCIExecute($statement, OCI_DEFAULT);
                $nrows = OCIFetchstatement($statement, $results);
                if ($nrows >
0)
                {
                        $out = '';

                        $fields = array("ID","CUSTOMERNAME","NUMBEROFSITES", "SATELLITE", "TRANSPONDER", "SERVICEPLAN", "PRODUCTTYPE", "NOTES");
                        $columns = 8 - 1;

                        for($i = 0; $i < $columns; $i++)
                        {
                                $l = $fields[$i];
                                $out .= $l.',';
                        }
                        $out .= $fields[7];
                        $out .= "\n";



                        if ($condition)
                        {      
                        print $out;
                                //exit();
                        }
                }

                elseif ($nrows == 0)
                {
                        echo "Sorry there are no customers to import csv file at this time.";
                }

                OCIFreeStatement($statement);

                //log off
                OCILogoff($connection);
        ?>

</div>

You could try making the

You could try making the link to csv script (only) as a standard href link?

<a class="b-step-overview b-panel" style="color:#999999;" href="transponderData/csv.php" target="_blank"><b>Download CSV file</b></a>

You would need to remove the DIV around the content though.
Pete

thanks it works

I really appreciate it