By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,359 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

Forcing the download box

P: n/a
Hello,
I have a page that generates a text file (but writes the file with
the ".csv" extension) and then I redirect the user so he can save the
file

header("Location: $my_csv_file");

However, the file appears as text in the browser and I would prefer
that the gray "Download this file" box pop up. How could I pull this
off, or is this really more dependent on client browser settings?

I appreciate any advice, - Dave
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
In article <9f**************************@posting.google.com >, D. Alvarado wrote:
Hello,
I have a page that generates a text file (but writes the file with
the ".csv" extension) and then I redirect the user so he can save the
file

header("Location: $my_csv_file");

However, the file appears as text in the browser and I would prefer
that the gray "Download this file" box pop up. How could I pull this
off, or is this really more dependent on client browser settings?


This question has been answered in the group many times before.
So type it in groups.google.com and see what the group had to say about
it.
--
Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>
Jul 17 '05 #2

P: n/a
D. Alvarado wrote:
I have a page that generates a text file (but writes the file with
the ".csv" extension)
Browsers ought to go by the media type of the Content-Type header, if
given, not any filename extensions or URI suffixes.
and then I redirect the user so he can save the file
ITYM, "so he may do with it what he wishes". ;o)
header("Location: $my_csv_file");

However, the file appears as text in the browser
I guess your server isn't configured to send a particular media type
for CSV files and its default is text/plain. Sending CSV files as
text/plain isn't ideal.
and I would prefer that the gray "Download this file" box pop up.
It may not always be possible for a grey "Download this file" box to
pop up; downloading and manually saving the file may not always be
desirable. Short of sending the boys round, you can't force it.
How could I pull this off,
The usual, RTFM-challenged suggestion is to work against the specs and
lie about the media type of the file. You'll find this is touted by
many, including some who've contributed to the Manual's user-
contributed notes, as *the* way.
or is this really more dependent on client browser settings?
Yes.
I appreciate any advice,


Tim's offered the best advice already.

Because there's no registered media type for CSV files, consider
sending a Content-Type header with a media type of text/x-csv. If a
media type is unknown to a browser, it should treat it as
application/octet-stream, which means "arbitrary binary data"
(RFC2046) -- IOW, no other media type was appropriate.

Alternatively, consider sending files in TSV format. There is a
registered media type text/tab-separated-values, and despite its name
the fgetcsv function handles tabs instead of commas just fine.

The protocol correct way of suggesting the presentational style of
data, however, is the Content-Disposition header (RFC2183). I doubt
if it's ever of any use on the web though.

Have a great weekend -- what's left of it!

--
Jock
Jul 17 '05 #3

P: n/a
*** D. Alvarado wrote/escribió (5 Jun 2004 11:42:09 -0700):
However, the file appears as text in the browser and I would prefer
that the gray "Download this file" box pop up. How could I pull this
off, or is this really more dependent on client browser settings?


header('Content-Disposition: attachment; filename=' . $suggestedfilename);

--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.