[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [cgiapp] How to download a dynamically generated file


Hi John,

John Crowley wrote:

Steve,

I believe the best way would be to redirect the user to a page which has its
headers set to the appropriate MIME type. In a simple (non-C::A) CGI, you can
use the following line...

Hmm. So I use CGI-Application to generate the file and then send a redirect header. The page that the user is redirected to is then handled by a non-CGI-Application CGI script that downloads that file, yes?

I would rather do it in one step rather than two, and all inside CGI-Application, if possible.

It seems a shame to have to step outside of CGI-Application to achieve something so simple.


[ generate your file ]
print header(-type => "text/comma-separated-values",
	     -attachment => "download.csv");

I had a look at the CGI.pm manpage, and it says this about the -attachment argument:

"The -attachment parameter can be used to turn the page into an attachment. Instead of displaying the page, some browsers will prompt the user to save it to disk. The value of the argument is the suggested name for the saved file."

That's not inappropriate behaviour for a ZIP file download, but would be annoying for PDF files which the browser normally "handles" by passing to the Adobe Reader.

So I prefer not to use the -attachment argument in those headers. The browser will still pop up a "Save As..." dialogue box anyway if it doesn't recognise the MIME type or has no action configured for it.


Then print the data to the browser.
For PDFs and ZIPs, I'm not sure if you can simply print, but let us know if
you succeed.

Yes, you can print anything you like to the browser as long as (1) you've set the correct Content-Type in the headers (using the -type argument) and (2) you've set both the filehandle that you're reading the file in from and STDOUT that you're writing the file out to to "binary mode" if you're on a platform like Windoze that does nasty CRLF translations otherwise.

- Steve


---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/suppressed/
             http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: suppressed
For additional commands, e-mail: suppressed


Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.