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

I have a script that is dynamically generating a text file for the user to download. When the user accesses the script, a pop-up box prompts them to save the text file to their hard drive. I use these headers to do so:
print "Content-disposition: attachment; filename=myletter.txt\n"; print "Content-Type: application/text\n\n"; print "Line 1\n\n"; print "Line 2\n\n";
The problem is that when I open up the downloaded text file on my hard drive, the newlines are not displayed correctly. They show up as boxes. What am I doing wrong? Any help would be greatly appreciated!

Replies are listed 'Best First'.
(bbfu) Re: Problem with newlines in text file generated for download
by bbfu (Curate) on Sep 06, 2001 at 21:03 UTC

    The cause of your problem is that, by using the application/* content-type, you are effectively telling the browser that the data is binary and local newline translation should not be done.

    Both suaveant's and trantor's suggests will probably work (at least, in the sort-term) but trantor's is (most likely) the better choice, as it will be more portable and will work if anyone ever downloads your text file onto Mac or *nix boxen. On the other hand, Internet Explorer likes to do some evil stuff and ignore Content-* headers at times, so changing the Content-Type to text/plain might cause IE to display the file instead of downloading it. You'll have to test it out to be sure.

    bbfu
    Seasons don't fear The Reaper.
    Nor do the wind, the sun, and the rain.
    We can be like they are.

Re: Problem with newlines in text file generated for download
by trantor (Chaplain) on Sep 06, 2001 at 20:51 UTC

    Would

    Content-Type: text/plain

    work better?

    -- TMTOWTDI

Re: Problem with newlines in text file generated for download
by suaveant (Parson) on Sep 06, 2001 at 20:43 UTC
    use \r\n instead of just \n for windows boxes...

                    - Ant
                    - Some of my best work - Fish Dinner