CGI, Zip and Downloading

Keary Suska hierophant at pcisys.net
Wed Jun 4 19:22:41 CDT 2003


on 6/4/03 2:08 PM, jtevans at kilnar.com purportedly said:

> 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.

I recommend this approach. If you plan to zip the files, you probably don't
have to worry about download issues. You can use Compress::Zlib to avoid
making a system call (and piping/file I/O issues). Your content type would
be application/zip (or application/x-gzip--either one should work since
content type only determines post-processing) and use a .zip extension.

Otherwise, you can instruct the browser on how to handle the file, which
should override default handlers:

print qq(Content-type: text/tab-separated-values;name="foo.txt"\n);
print qq(Content-Disposition: attachment;filename="foo.txt"\n\n);

Note that some versions of IE are buggy with handling Content-Disposition.

If that doesn't work, you can use a bogus mime type:
x-download/x-custom-data-file

Though this approach either requires pre-configuration of all browsers or
that the end user picks the correct action when asked (download only).

Keary Suska
(719) 473-6431
(719) 440-9952 (cell)




More information about the Pikes-peak-pm mailing list