Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #7210 > unrolled thread

Pls help with output of csv file to browser?

Started byBobMCT <r.mariotti@fdcx.net>
First post2012-09-30 11:19 -0400
Last post2012-10-02 03:41 +0000
Articles 7 — 7 participants

Back to article view | Back to comp.lang.php


Contents

  Pls help with output of csv file to browser? BobMCT <r.mariotti@fdcx.net> - 2012-09-30 11:19 -0400
    Re: Pls help with output of csv file to browser? "Peter H. Coffin" <hellsop@ninehells.com> - 2012-09-30 10:41 -0500
    Re: Pls help with output of csv file to browser? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-30 19:01 +0200
    Re: Pls help with output of csv file to browser? bill <william@TechServSys.com> - 2012-10-01 08:16 -0400
      Re: Pls help with output of csv file to browser? The Natural Philosopher <tnp@invalid.invalid> - 2012-10-01 23:28 +0100
    Re: Pls help with output of csv file to browser? Jerry Stuckle <jstucklex@attglobal.net> - 2012-10-01 14:50 -0400
    Re: Pls help with output of csv file to browser? Denis McMahon <denismfmcmahon@gmail.com> - 2012-10-02 03:41 +0000

#7210 — Pls help with output of csv file to browser?

FromBobMCT <r.mariotti@fdcx.net>
Date2012-09-30 11:19 -0400
SubjectPls help with output of csv file to browser?
Message-ID<h7og68tjuulikujgrv1k7usbtue38a4s69@4ax.com>
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 

[toc] | [next] | [standalone]


#7211

From"Peter H. Coffin" <hellsop@ninehells.com>
Date2012-09-30 10:41 -0500
Message-ID<slrnk6gq24.4ja.hellsop@nibelheim.ninehells.com>
In reply to#7210
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

[toc] | [prev] | [next] | [standalone]


#7213

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-09-30 19:01 +0200
Message-ID<3653659.dzkcXP51QQ@PointedEars.de>
In reply to#7210
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).

[toc] | [prev] | [next] | [standalone]


#7215

Frombill <william@TechServSys.com>
Date2012-10-01 08:16 -0400
Message-ID<k4c1ji$1vc$1@speranza.aioe.org>
In reply to#7210
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).

[toc] | [prev] | [next] | [standalone]


#7217

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2012-10-01 23:28 +0100
Message-ID<k4d5dp$mf7$1@news.albasani.net>
In reply to#7215
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.

[toc] | [prev] | [next] | [standalone]


#7216

FromJerry Stuckle <jstucklex@attglobal.net>
Date2012-10-01 14:50 -0400
Message-ID<k4colp$n93$1@dont-email.me>
In reply to#7210
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@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#7219

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2012-10-02 03:41 +0000
Message-ID<k4dnpp$3bb$2@dont-email.me>
In reply to#7210
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web