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!


In reply to Refresh page then download from a cgi program by relax99

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.