Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Prompt image save

by DreamT (Pilgrim)
on Nov 11, 2011 at 15:12 UTC ( [id://937594]=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://937594]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-25 09:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found