in reply to saving data locally in CGI script?

use strict; use warnings; use CGI; my $cgi = CGI->new(); sub handle_error { print $cgi->header( -status => 500, -type => 'text/html', ); print ...; exit(0); } open(my $fh, '<', 'foo.csv') or handle error(...); print $cgi->header( # application/octet-stream might work better -type => 'text/csv', -attachment => 'foo.csv', ); print while <$fh>;

Of course, you can base the name of the CSV on a CGI parameter or on a session field. Or you can generate it on the fly instead of reading it form disk.

Replies are listed 'Best First'.
Re^2: saving data locally in CGI script?
by Anonymous Monk on Jan 22, 2010 at 23:04 UTC

    Thanks! Your idea made this even more compact:

    my $cgi = CGI->new(); my $s = "Hi, Mom!\n"; print $cgi->header(-type => 'text/csv', -attachment => 'filename.c +sv', -Content-length => length($s)); print $s;

    Is it possible to generate the standard file-open dialog such that the filename can be changed?

      To pick the name of the file to which to save? Your browser should do that already.

        Correct. The browser prompts to save the file using the (default) filename supplied by the server. Yet the standard file-open dialog (which is different than the one used here...) will allow the user to rename the file. How can I get this functionality?

        Thanks.