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

Here is the code -
print $cgi->header( -meta => { 'HTTP-EQUIV' => 'refresh', 'CONTENT' => '5', 'URL' => 'http://url/file. +xls' }); print $cgi->start_html(); print "<h1>HTML Body</h1>"; print $cgi->end_html();
I am trying to print some html, and 5 seconds later download a file. Instead of downloading the file, it prints out the file in html. My feeling is this has to do with the default header content-type that specifies html. So what I end up seeing is binary data printed out in my browser, plus the html I am trying to print. What I want is to print out the html, and then have a popup screen that allows me to download the file. Problem is I need to specify two content-types it seems. Can this be done as a multipart document? Does anyone have any code examples? Help is greatly appreciated... ;0)

Replies are listed 'Best First'.
Re: Cannot download and print html
by hardburn (Abbot) on Jul 14, 2004 at 19:06 UTC

    Sounds like your server isn't sending the correct MIME type for the file you're redirecting to. There's nothing magic about the request of a meta redirect as far as HTTP is concerned.

    ----
    send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

Re: Cannot download and print html
by rjbs (Pilgrim) on Jul 14, 2004 at 19:09 UTC
    I'm not big on CGI.pm, but I don't know of a way to put those values in the HTTP headers. Instead, you can put them in the HTML head. I assume that's what you meant, anyway, since you said "http-equiv", which is for the META element.

    Search the CGI.pm perldoc for -head.
    use CGI qw(:all); my $cgi = CGI->new; print $cgi->header; print $cgi->start_html( -head => meta( { 'http-equiv' => 'refresh', 'content' => '5', 'url' => 'http://url/file.xls' })); print "<h1>HTML Body</h1>"; print $cgi->end_html();
    rjbs

      You covered yourself by stating that you are not big on CGI.pm, but i really feel like i should nitpick as this is a very common mistake:

      use CGI qw(:all); my $cgi = CGI->new;
      That is mixing the two styles you use CGI as: procedural versus OO. Always use one, but never use both. That is, you either use this:
      # import CGI's functions into my namespace use CGI qw(:all); print header, start_html( ... ), h1('HTML Body'), end_html;
      or you use this:
      use CGI; # access CGI's functions as object methods my $cgi = CGI->new; print $cgi->header ... (yadda yadda)
      Cheers. :)

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
        You're quite right -- I just added that in annoyance when I realized that CGI wasn't exporting the meta sub, which provides the proper value for the -head parameter. I should have used CGI::meta, since I'm not sure how to get at it OO-ly. Or perhaps there is a subset export to just get things that are not available as methods? Enlighten me, frater!
        rjbs
      Refresh can be passed as an HTTP header. META HTTP-EQUIV is a way to set HTTP header equivalents in the HTML code. With CGI, the Refresh HTTP header can be set directly.
      print $cgi->header(-refresh => '5; http://url/file.xls');