relax99 has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I was wondering if anyone knew of the best way to output a refreshed page from a cgi program and then start a file download to the user's computer.

Basically, I have a Perl cgi program that creates new excel files on the fly, based on the information submitted in the form. The form contains a browser file upload field to let the user submit the data. What I want to do is first refresh the webpage to provide additional information to the user after they submit the form and then start the download.

I looked around, but the only two things I could find that were similar to what I am doing were:

1. When the user submits the form, insert form submit code into the form's onLoad() event, so that when the webpage refreshes it would submit itself again, this time indicating to the cgi program that it should start the required download.

Problems with this approach:

2. Have a meta refresh tag in the html that points to the url of the file to be downloaded.

Problems with this approach:

Here's some source code that shows what I am doing.
# create excel file my ($fh, $xls_filename) = tempfile(DIR => $tempfile_path, SUFFIX => ". +xls", UNLINK => 1); my $xls_workbook = Spreadsheet::WriteExcel->new($xls_filename); my $xls_worksheet = $xls_workbook->add_worksheet(); ### inserting data into the worksheet $xls_workbook->close(); my $download_filename = "standard_filename.xls"; print "Content-Title: $download_filename\n"; print "Content-Disposition: attachment; filename=$download_filename\n" +; print "Content-Type: application/octet-stream; file=$download_filename +\n\n"; open(XLS, $xls_filename) or die "can't open temporary excel file: $!\n +"; binmode XLS; binmode STDOUT; my $buff; while( read(XLS, $buff, 1024) ) { print STDOUT $buff; } close(XLS); exit(0); # exit for now - without refreshing the screen

Any insights, anyone?

Thanks!

Replies are listed 'Best First'.
Re: Refresh page then download from a cgi program
by sgifford (Prior) on Aug 09, 2004 at 20:24 UTC
    The URL of the file to be downloaded can be a script. So the first form could submit to script1, which would print the desired information then redirect to script 2, which would produce the Excel document.
Re: Refresh page then download from a cgi program
by roju (Friar) on Aug 09, 2004 at 20:17 UTC
    If the excel files are small, or your users have lots of bandwidth, you could pass the excel file back to the user in a hidden field, and use a refresh. If the hidden field has data, you pipe it back as an excel file.

    Granted, this requires thrice the bandwidth, and you'd have to encode/decode the spreadsheet to fit it in the html, but it'd be relatively easy to implement ;). That said, I'd suggest finding a better approach.