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

Masked Monks,

After generating a csv file with my Perl script, I want to download it to the client. No problem, you say, just use http headers:

print "Content-type: text/csv"; print "Content-Disposition: attachment; filename=my_file.csv\n\n";
Which I've done, and the file does download, BUT, the filename is given as the name of the Perl script file, not the filename I set, my_file.csv.

What silly thing have I forgotten?

Thanks.




Forget that fear of gravity,
Get a little savagery in your life.

Replies are listed 'Best First'.
Re: Output CSV-file, can't get filename recognized.
by moritz (Cardinal) on Jan 30, 2008 at 18:54 UTC
    Have you tried it with different browsers? User agents are pretty much free to ignore any Content-Disposition header.

    BTW this post talks about \n being possibly harmful.

      Genius. Pure genius.

      The post you linked to reminded me to check my "\n", and as you'll see on double-checking my original post, I forgot a "\n" at the end of the first line. Fixing that made it work.

      Thanks.

        ... and the missing newline explains exactly what happened:
        The browser treated everything up to the first newline as a content-type, didn't recognize it, and therefore opened as "save as" dialog instead of displaying it.

        And it had no filename specification, so it used the script name from the URL.