473,396 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Forcing the download box

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
3 1752
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
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
*** 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: lmb | last post by:
Hi all, Does anyone know how to force the download of a text file ? In my script, I tried : header("location: my_text_file.txt") ; ....and of course, I get my text in my browser as I wanted a...
1
by: thegump | last post by:
I have a href link and its a MP3 file and I want to download it to the clients machine but when I click on it, it wants to play it and not download it. How do I stop this. I was told to change the...
3
by: Julie | last post by:
I have an html file where I display name,address,zip. It is one per line so it is basicall a list of addresses. But I would like ie6 to force the download prompt of this long list of names...
1
by: Do | last post by:
Hi, My issue is twofold. 1) How do I invoke a "save file" action when a user clicks a button. In classic ASP, I used to response.redirect to file path. But I want to keep the user on the...
4
by: randmCP | last post by:
Hi and thanks in advanced for you help. I am working on a file-distribution asp.net application. Users click on a custom grid column to download files stored on a SQL DB. The client does not want...
1
by: Brett Kelly | last post by:
Ok, I know this sounds odd. Let me explain further. I have an ASP.net page (w/ C# code behind) that, when given a session variable containing the path to a local file, will attempt to start the...
4
by: teeBull | last post by:
Hi all, We'd like to take advantage of code we already have for transforming XML into HTML (using XSLT) for our users to save the HTML as an MS Word document locally. I've dug around and found...
18
by: Zytan | last post by:
I just got a TypeInitializationException exception. Why it is a common cause for this? The InnerException is System.FormatException, although I don't see anything requesting something to be...
14
by: Irfan12 | last post by:
i dont know php & i want a script that when a user click on a mp3 hyperlink, the mp3 file start downloading instead of start playing in media player. i got a sxript from internet search. i am using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.