Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Not able to display 'save as dialog box' in browser

by perlCrazy (Monk)
on Oct 03, 2007 at 07:03 UTC ( #642317=perlquestion: print w/replies, xml ) Need Help??
perlCrazy has asked for the wisdom of the Perl Monks concerning the following question:

I think this line is causing the problem. No worries, I wil take care of this now.
print "Content-Length: $filesize\n";

Thank you all

Replies are listed 'Best First'.
Re: Not able to display 'save as dialog box' in browser
by naikonta (Curate) on Oct 03, 2007 at 08:04 UTC
    print "Content-Type: application/octet-stream\n\n";
    You can only use a single CRLF character as header separator. Here, you use two. Two CRLF character is used to separate headers from content.

    After all, how can you have two Content-Type header? If what you have is a plain text file to send, you don't need that octet-stream. I think you want,

    print "Content-Type: text/plain\n"; print "Content-Length: $filesize\n"; print "Content-disposition: attachment; filename=$filename\n\n"; open FILE, $filename or die "can't open $filename: $!\n"; print while <FILE>; close FILE;
    Using \n as CRLF can be problematic. It's safer to use CGI or CGI::Simple, CRLF is handled automatically.
    use CGI::Simple; use strict; use warnings; my $cgi = CGI::Simple->new; my $filename = '/usr/local/..../file.txt'; # assumed exists print $cgi->header( -type => 'text/plain', -attachment => $filename, -content_length => -s($filename), ); open FILE, $filename or die "Can't open $filename: $!\n"; print while <FILE>;

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: Not able to display 'save as dialog box' in browser
by erroneousBollock (Curate) on Oct 03, 2007 at 07:42 UTC
    sub downloadFile { # $filepath is the directory # $filename is the name of the file my ($filepath,$filename) = @_; chdir($filepath) || return(0); my $filesize = -s $filename; # print full header print "Content-Type: application/octet-stream\n\n"; print "Content-Length: $filesize\n"; print "Content-disposition: attachment; filename=$filename\n\n"; print "Content-type: text/plain\n\n"; # open in binmode open(READ,$filename) || die; binmode READ; # stream it out binmode STDOUT; while (&lt;READ&gt;) { print; } close(READ); # should always return true return(1); }
    First (most importantly), only the first Content-Type header is being seen by the browser (none of the following headers) because you followed it with '\n\n'. Only put two newlines on the last header sent to the browser (or let something like CGI do it for you).

    Also, you are sending two Content-Type headers for the same data. text/plain would probably be appropriate for the dbschema.txt file.

    binmode() is also probably unnecessary.


Re: Not able to display 'save as dialog box' in browser
by misc (Pilgrim) on Oct 03, 2007 at 09:15 UTC
    Try to debug your headers.
    Even if you print the headers the right way, there's still the possibility your web server (apache?) prints some headers.
    I'm using firebug (,
    but you could also telnet to your webserver.

    telnet localhost 80 GET /theurl

    I'm also using mod_rewrite with apache.
    This way you can, e.g., "redirect" all urls beginning with http://www.yourserver/download/ to your cgi script.

    If you put out pdf files, for example, the adobe reader will not recognize the correct filename otherwise, the button "save a copy" would save the file as "".

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://642317]
Approved by naikonta
and one hand claps...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2018-06-19 01:33 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.