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


Groups > comp.lang.php > #7217

Re: Pls help with output of csv file to browser?

From The Natural Philosopher <tnp@invalid.invalid>
Newsgroups comp.lang.php
Subject Re: Pls help with output of csv file to browser?
Date 2012-10-01 23:28 +0100
Organization albasani.net
Message-ID <k4d5dp$mf7$1@news.albasani.net> (permalink)
References <h7og68tjuulikujgrv1k7usbtue38a4s69@4ax.com> <k4c1ji$1vc$1@speranza.aioe.org>

Show all headers | View raw


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.

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web