in reply to Perl CGI download file and redirect

You need to reverse the order of your steps. Your current approach is

  1. Send the file to download (which is not HTML)
  2. Display the "thank you for downloading" page (which is HTML)

Instead of doing it in that order, do it the other way around

  1. Display the "thank you for downloading" page (which is HTML)
  2. Issue a redirect or popup to the real download page
  3. Send the file to download (which is not HTML)
  • Comment on Re: Perl CGI download file and redirect

Replies are listed 'Best First'.
Re^2: Perl CGI download file and redirect
by Yaerox (Scribe) on Nov 17, 2014 at 14:25 UTC

    Didn't I have to print headers first? If I print the header the file download is starting directly. How am I supposed to print first html then starting the download?

      Oh, sorry - I glossed over that part. You need a separate URL for sending the download. There you send the download headers. You have the URL for the "thank you" HTML page. There, you display the "thank you" message and then redirect (using META tags or Javascript) to the download page:

      <html> <head> <meta http-equiv="refresh" content="5; url=download.pl?filename=$p_fil +ename.pdf"> <body> <h1>Thank you!</h1> </body> </html>

        I'm reffering from a script (let's call it main.pl) printing:

        <html> <head> <title>Redirect</title> <meta http-equiv="refresh" content="0; URL=download.pl?filename=$f +ilename"> </head> </html>
        My download.pl is what I posted on my first post:
        use strict; use warnings; use CGI; my $oCGI = new CGI; my $p_filename= $oCGI->param("filename"); print "Content-Type: application/x-download\n"; print "Content-Disposition: attachment; filename=$p_filename.pdf\n\n"; unlink $p_filename;
        So I'm going to call main.pl -> redirecting to download.pl and now I want to redirect from download.pl to justanotherscript.pl. But when redirecting to justanotherscript.pl I need to get the file download on the same step.