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

Hi all, I have an otherwise perfectly functioning PDF download script which is producing some serious weirdness on the filename front: despite the headers having the Content-disposition set to "attachment", with a correct filename declared, the filename always comes out when saving the file in a browser as "http__<domain name minus the TLD>.pdf". Anyone got any ideas as to what could be causing this? The problem code:
open (PDF_FILE,"<$pdffile") || die "Could not open file: $pdffile"; print $cgi->header(-type=>"application/pdf",-attachment=>"$pdfid.pdf") +; # set the appropriate header while (<PDF_FILE>) { # send the PDF bitstream to the client print; } close PDF_FILE;
Thanks.

Replies are listed 'Best First'.
Re: CGI download filename weirdness
by ikegami (Patriarch) on Feb 08, 2010 at 16:58 UTC
    • It works for me, for $pdfid = "foo". What browser are you using?

    • .pdf aren't text files, so you don't want them mangled. Add binmode(PDF_FILE);.

    • .pdf aren't text files, so trying to read lines makes no sense. Add local $/ = \(64*1024);.

Re: CGI download filename weirdness
by kennethk (Abbot) on Feb 08, 2010 at 15:54 UTC
      Thanks for the response. $pdffile is a path to the "physical" file, and $pdfid is the name of that file, without the extension.

      I just tried pulling out this code into a separate script and giving these variables hard-coded values, and it works fine there, so it seems to be caused by something elsewhere than in the snippet I posted. This little PDF subroutine is part of a monstrous pile of code, thousands of lines long, and a real nightmare to debug.

      A perhaps related issue is that on some browsers, the download never quite gets wrapped up properly - it gets left with a ".part" extension which needs to be removed manually in order to use an otherwise fine file.

      Still digging away at this...

        I was trying to imply that $pdfid does not contain what you think it does. You should be able to do a simple file search for $pdfid to see where it was last modified. I highly doubt this is a browser issue.