Pls help with output of csv file to browser? [message #179280] |
Sun, 30 September 2012 15:19 |
r.mariotti
Messages: 17 Registered: December 2011
Karma: 0
|
Junior Member |
|
|
I'm trying to add the generation of a csv file to send to the
requestor's browser. So far I do have it working (sort of) by sending
special headers indicating a text file and using fputcsv. The user
receives the expected what to do prompt and if saved the dialog box
asking where to save it appears and works.
That's the good part.
The bad part is that there is no resulting/completion page. And, if I
examine the saved csv file it actually contains the html for the
subsequent page and the originating page still resides on the
requestor's screen.
My guess would be that once the file is sent another set of header
details must be sent to tell the browser that html is now coming.
But, I cannot locate any examples of what to send in this case.
I'm hoping that some of you readers have done this before and can
share the technique/mechanism used to accomplish this successfully.
Any ideas/examples/suggestions greatly appreciated.
Thank
|
|
|
Re: Pls help with output of csv file to browser? [message #179281 is a reply to message #179280] |
Sun, 30 September 2012 15:41 |
Peter H. Coffin
Messages: 245 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On Sun, 30 Sep 2012 11:19:09 -0400, BobMCT wrote:
> I'm trying to add the generation of a csv file to send to the
> requestor's browser. So far I do have it working (sort of) by sending
> special headers indicating a text file and using fputcsv. The user
> receives the expected what to do prompt and if saved the dialog box
> asking where to save it appears and works.
>
> That's the good part.
>
> The bad part is that there is no resulting/completion page. And, if I
> examine the saved csv file it actually contains the html for the
> subsequent page and the originating page still resides on the
> requestor's screen.
>
> My guess would be that once the file is sent another set of header
> details must be sent to tell the browser that html is now coming.
> But, I cannot locate any examples of what to send in this case.
>
> I'm hoping that some of you readers have done this before and can
> share the technique/mechanism used to accomplish this successfully.
>
> Any ideas/examples/suggestions greatly appreciated.
I've not done this, but if you send the starting page, have that open up
a new tab-page-whatever that handles the download, and then the display
page uses javascript to query the server occasionally (or listens for a
semaphore) to find out when the CSV-sender program has finished, then
the displayed page can trigger the refresh to the completion page.
--
Remember, a 12'x12'x18" raised floor can hold over a thousand gallons of
blood before it starts to seep up through the cracks.
-- Roger Burton West in the Monastery
|
|
|
Re: Pls help with output of csv file to browser? [message #179283 is a reply to message #179280] |
Sun, 30 September 2012 17:01 |
Thomas 'PointedEars'
Messages: 701 Registered: October 2010
Karma: 0
|
Senior Member |
|
|
BobMCT wrote:
^^^^^^
Who?
> I'm trying to add the generation of a csv file to send to the
> requestor's browser. So far I do have it working (sort of) by sending
> special headers indicating a text file and using fputcsv. The user
> receives the expected what to do prompt and if saved the dialog box
> asking where to save it appears and works.
>
> That's the good part.
>
> The bad part is that there is no resulting/completion page.
There is not supposed to be one. The moment the download starts, control is
turned over to the client which hopefully (and usually) provides the feature
to show the progress of the download.
> And, if I examine the saved csv file it actually contains the html for
> the subsequent page and the originating page still resides on the
> requestor's screen.
That is BAD. Borken As Designed.
> My guess would be that once the file is sent another set of header
> details must be sent to tell the browser that html is now coming.
> But, I cannot locate any examples of what to send in this case.
>
> I'm hoping that some of you readers have done this before and can
> share the technique/mechanism used to accomplish this successfully.
>
> Any ideas/examples/suggestions greatly appreciated.
It should be possible to send an HTTP multi-part response message as defined
in <http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.2>.
However, since that might not be supported, you should include a link for
downloading *only* the CSV in the HTML part anyway.
That is, if you absolutely must display an HTML document along with the CSV
download. The simpler way is not so send any HTTP response containing
*HTML* on success at all, for example:
<?php
/* process data */
if ($success)
{
header('Content-Type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename=my.csv');
fputcsv('php://stdout', …);
}
else
{
header('Content-Type: text/html; charset=UTF-8');
require_once 'error.php';
}
?>
You may also use, but should never rely on, client-side processing.
PointedEars
--
When all you know is jQuery, every problem looks $(olvable).
|
|
|
Re: Pls help with output of csv file to browser? [message #179285 is a reply to message #179280] |
Mon, 01 October 2012 12:16 |
bill
Messages: 310 Registered: October 2010
Karma: 0
|
Senior Member |
|
|
On 9/30/2012 11:19 AM, BobMCT wrote:
> I'm trying to add the generation of a csv file to send to the
> requestor's browser. So far I do have it working (sort of) by sending
> special headers indicating a text file and using fputcsv. The user
> receives the expected what to do prompt and if saved the dialog box
> asking where to save it appears and works.
>
> That's the good part.
>
> The bad part is that there is no resulting/completion page. And, if I
> examine the saved csv file it actually contains the html for the
> subsequent page and the originating page still resides on the
> requestor's screen.
>
> My guess would be that once the file is sent another set of header
> details must be sent to tell the browser that html is now coming.
> But, I cannot locate any examples of what to send in this case.
>
> I'm hoping that some of you readers have done this before and can
> share the technique/mechanism used to accomplish this successfully.
>
> Any ideas/examples/suggestions greatly appreciated.
>
> Thank
>
What I do (which is not a recommendation ;-) is:
using javascript I have the originating page (the page before the
output):
1. the button (or whatever) opens a new page, using
window.open('new_script.php?args_as_necessary')
2. then submits
the window.open invokes new_script.php that actually generates
the csv and send it with the appropriate headers. Firefox will
put the csv in a new tab on top.
the submit of the originating page generates and sends the
subsequent page (which probably will be under the csv)
the user saves/prints the csv and closes that tab and the
subsequent page pops up.
Yeah it requires javascript, not a problem in my setting (intranet).
|
|
|
Re: Pls help with output of csv file to browser? [message #179286 is a reply to message #179280] |
Mon, 01 October 2012 18:50 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 9/30/2012 11:19 AM, BobMCT wrote:
> I'm trying to add the generation of a csv file to send to the
> requestor's browser. So far I do have it working (sort of) by sending
> special headers indicating a text file and using fputcsv. The user
> receives the expected what to do prompt and if saved the dialog box
> asking where to save it appears and works.
>
> That's the good part.
>
> The bad part is that there is no resulting/completion page. And, if I
> examine the saved csv file it actually contains the html for the
> subsequent page and the originating page still resides on the
> requestor's screen.
>
> My guess would be that once the file is sent another set of header
> details must be sent to tell the browser that html is now coming.
> But, I cannot locate any examples of what to send in this case.
>
> I'm hoping that some of you readers have done this before and can
> share the technique/mechanism used to accomplish this successfully.
>
> Any ideas/examples/suggestions greatly appreciated.
>
> Thank
>
Not possible the way you're trying to do it. A request to the server
can return exactly one content-type. It can be an image, a web page
(i.e. html), a document (i.e. .csv) or whatever. You are trying to
return two different types (a .csv followed by a .html) in the same
request. It's not allowed.
You also can't send javascript or an html redirect in a .csv file - the
browser will just consider that part of the .csv file. And you can't
send an http redirect followed by the .csv file.
IOW, your design won't work.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
Re: Pls help with output of csv file to browser? [message #179287 is a reply to message #179285] |
Mon, 01 October 2012 22:28 |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
bill wrote:
> On 9/30/2012 11:19 AM, BobMCT wrote:
>> I'm trying to add the generation of a csv file to send to the
>> requestor's browser. So far I do have it working (sort of) by sending
>> special headers indicating a text file and using fputcsv. The user
>> receives the expected what to do prompt and if saved the dialog box
>> asking where to save it appears and works.
>>
>> That's the good part.
>>
>> The bad part is that there is no resulting/completion page. And, if I
>> examine the saved csv file it actually contains the html for the
>> subsequent page and the originating page still resides on the
>> requestor's screen.
>>
>> My guess would be that once the file is sent another set of header
>> details must be sent to tell the browser that html is now coming.
>> But, I cannot locate any examples of what to send in this case.
>>
>> I'm hoping that some of you readers have done this before and can
>> share the technique/mechanism used to accomplish this successfully.
>>
>> Any ideas/examples/suggestions greatly appreciated.
>>
>> Thank
>>
> What I do (which is not a recommendation ;-) is:
> using javascript I have the originating page (the page before the output):
> 1. the button (or whatever) opens a new page, using
> window.open('new_script.php?args_as_necessary')
> 2. then submits
>
> the window.open invokes new_script.php that actually generates the csv
> and send it with the appropriate headers. Firefox will put the csv in a
> new tab on top.
>
> the submit of the originating page generates and sends the subsequent
> page (which probably will be under the csv)
>
> the user saves/prints the csv and closes that tab and the subsequent
> page pops up.
>
> Yeah it requires javascript, not a problem in my setting (intranet).
In my main page (www.gridwatch.templar.co.uk) there is a button to
download a CSV file
Th button is called in te page usiing this
web_button(200,20,"Download", "Download Data","/downloads/grid.csv");
function that does the button presentation is this
function web_button($x,$y,$label, $alt,$url)
{
?>
<div class="button" onmouseover="this.className='hot-button'"
onmouseout="this.className='button'"
style="Position: absolute; top:<?echo $y;?>px; left:<?echo $x;?>px;
height: 35px; width: 150px; text-align:center;">
<a class="mybutton" href="<?echo $url?>"><? echo $label?></a>
</div>
<?
}
You can grab the CSS file probably as well from this site if you like
the button.
Now this csv file is built every hour by cron, from the database. If you
wanted to instead have a button that constructs the data on an as
requested basis, the "/downloads/grid.csv" needs replacing with a
pointer to your script.
That script should send headers consistent with the data being a CSV,
and then the data.
And nothing else.
in general the browser will respond to data which is not displayable -
i.e. not an image or HTML - as defined by the headers, by either
inviting you to download it and save it, or invoking some application
that it thinks will recognise it, or by passing it to a plugin to handle it.
I.e. it will not necessarily *follow* (in the sense of replacing the
current web page) a hyperlink to downloadable data
I hope this helps. If you have more queries ask here.
--
Ineptocracy
(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.
|
|
|
Re: Pls help with output of csv file to browser? [message #179289 is a reply to message #179280] |
Tue, 02 October 2012 03:41 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On Sun, 30 Sep 2012 11:19:09 -0400, BobMCT wrote:
> I'm trying to add the generation of a csv file
When I want to output dynamically generated csv data to a user (such as
the raw output from a mysql db query to use in a spreadsheet), I include
a link to a php file that generates the csv file eg:
<a href="query6csv.php">get csv data here</a>
This dynamically generates csv data and sends it with the appropriate
mime type (ie text/csv) using the php header function, it also sends a
content disposition header with a suggested filename to save the file as.
<?php // start of file - must be at very start of line 1!
header( "Content-Type: text/csv" );
header( "Content-Disposition: attachment; filename=\"fname.csv\"" );
/*
generate and send send csv data here
*/
// end of file - do not use question mark + right angle bracket!
If your csv file is pre generated, just link to the csv file <a
href="somefile.csv">get csv data here</a>, and make sure your server
serves files of ".csv" as mime type "text/csv".
Note that it's important to have no white space at all preceding the
opening "<php" in the dynamically generated csv case, as the presence of
white space before the php code will trigger standard headers to be sent.
Likewise, don't terminate the php code with "?>" as if you do, anything
after that termination will get tacked onto the end of your csv data
file, possibly rendering it useless to the recipient uinless they
manually clean it up first.
Rgds
Denis McMahon
|
|
|