CGI, Zip and Downloading

John Evans jtevans at kilnar.com
Wed Jun 4 15:08:20 CDT 2003


Heya guys,
	I figured that I'd throw a Perl problem your way as a send off for 
Tim (although, I'm not exactly sure when he's leaving.)

	Here's the problem....

	I'm displaying some contact information from a contact manager on 
a web-site. My slave drivin' boss (j/k) also wants the data to be 
available in a single file that contains one record on each line and each 
field to be tab-seperated.

	The file has to have the extension ".txt" so that our users will 
regocnize it. The file has to be downloaded right off the bat, not 
displayed in the web browser. Unfortunately, the instructions "right-click 
/ save as" are too complex for our users.

	I've got the data formatted correctly. That was cake. The issue is 
coughing up the data in a format that the web browser will accept and save 
it. Unfortunately, the data can change at any second, so each time the 
file is downloaded, it must be generated. There are two solutions that I 
see to the problem. I'm sure there are more, but here's the way I plan to 
attack it.

1) Create a directory in Apache's DocumentRoot that is writable by the web 
server. Create the files (with a large random number in the filename) and 
drop them into this directory, the redirect the web browser to the data 
using HTTP headers.
	Here's the problems with this one:
	a) I hate allowing the web server to write to the filesystem. 
Gives me the ooglies.
	b) Using the "Location: " header gets the file to the person, but 
it has to be named something like foo.tsv so that Apache will use the MIME 
type "text/tab-separated-values" so that the web browser will download the 
file instead of display it.
	c) I could create the file and use a system() call to zip it up, 
thus getting a downloadable file and use the "Location: " header, but that 
requires my users to have and know how to use something like WinZip. 
Unfortunately, WinZip is probably right at the threshhold for complexity 
that my users can handle.
	d) Using the "URI: " header allows me to specify a location and a 
content-type (so I can name the file "foo.txt", but still get the 
"text/tab-separated-values" MIME-type.) However, this does not seem to be 
working. I either get "Premature end of script headers..." or nothing at 
all. Here's the line that I'm using (the line splits, assume a single 
space between the ';' and the "vary"):
print "URI:  http://$server/lists/$filename;  
vary=text/tab-separated-values\n\n";


2) Create the file on the fly and toss a the file with a Content-type that 
forces all browsers (with default configs) to download the file instead of 
display it.
	Problems with this solution:
	a) I haven't a clue on which Content-type may do this.
	b) The default filename is always the name of the script. Again, 
telling the users to rename the file "contacts.txt" is beyond their 
abilities.


Thanks for any insight that you may be able to bring to the problem.


-- 
John Evans
http://jtevans.kilnar.com/

-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS d- s++:- a- C+++>++++ ULSB++++$ P+++$ L++++$
E--- W++ N+ o? K? w O- M V PS+ !PE Y+ PGP t(--) 5-- X++(+++)
R+++ tv+ b+++(++++) DI+++ D++>+++ G+ e h--- r+++ y+++
------END GEEK CODE BLOCK------




More information about the Pikes-peak-pm mailing list