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

Hi, I've created a cgi-script that creates a png image, and I want the browser to prompt the user to download it (rather than displaying it). How can I do it?

Replies are listed 'Best First'.
Re: Prompt image save
by liverpole (Monsignor) on Nov 11, 2011 at 15:54 UTC
    Hi DreamT,

    This works for me:

    #!/usr/bin/perl -w use strict; use warnings; use CGI::Carp qw{ fatalsToBrowser }; use IO::File; my $png = "test.png"; my $fh = new IO::File($png) or die "Can't read file '$png' ($!)\n" +; my $nbytes = (-s $png); binmode $fh; my $data; sysread($fh, $data, $nbytes) or die "Failed to read data from '$png' ( +$!)\n"; print "Content-disposition: attachment; filename=$png\n\n"; print $data;

    Update:  Removed '...' from .png filename in header.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Prompt image save
by moritz (Cardinal) on Nov 11, 2011 at 15:37 UTC
Re: Prompt image save
by ChuckularOne (Prior) on Nov 11, 2011 at 17:13 UTC
    You could just forward to a URL that ends in the image name and let the browser prompt for download.

    Update...
    Never mind, that will just load the image.

Re: Prompt image save
by Anonymous Monk on Nov 11, 2011 at 15:23 UTC

    That's really more of a browser question shading into HTTP/HTML, rather than a perl question. Somebody here might know, but searching web design sites would probably get you answers quicker.