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

Good afternoon,

I am trying to deliver an image to a client PC for whatever they may wish to do with it. After completing my purchasing process, I want to send the image to them to save or print. I have tried several methods of doing so (thank you, Super Search!) but can't seem to get either of them to work for me.

I have tried the following methods of doing this:

my $file = "\\\\cromedome\\images\\182.TIF"; print $request->header(-type=>'image/tif', -attachment=>$file);
and
my $file = "\\\\cromedome\\images\\182.TIF"; my $buffer; open TIF, $file or die "Cant open image"; read TIF, $buffer, -s(TIF); print $request->header(-type=>"image/tif"), $buffer;
When the CGI executes, I get a dialog to "Open" or "Save As". Open lets me select my image viewer, but whatever data is being delivered to it is not my image.

One other trick: I'd like the file delivered in such a way (perhaps with a .TIF extension?) so Windows will know to pop open the default image viewer when I select open.

FWIW, clients will be using IE 5.5 and above, and likely Windows 2k. That's what I've been testing this with. Any insight is appreciated :)

Thanks!
MrCromeDome

Replies are listed 'Best First'.
Re: Delivering an image to a client browser/PC
by fglock (Vicar) on Sep 03, 2002 at 18:42 UTC

    delivered in such a way (perhaps with a .TIF extension?)

    You can do this with a content-disposition header

    Did you try  binmode the file?

    Try also  do { local $/; $img = <TIF> };

      Tried to binmode the file prior to reading it, but it didn't change the outcome. I also tried substituting in the code you suggested, but again, the outcome was undesirable.

      How would that content-disposition header look? Outside of cookies and redirects, I've never had occasion to alter the header before.

      Thanks for your assistance,
      MrCromeDome

Re: Delivering an image to a client browser/PC
by zaimoni (Beadle) on Sep 03, 2002 at 23:08 UTC

    This fragment is from a login-protection script I wrote to test a system I'm eventually planning to deploy on my own domain. It works on my server's FreeBSD installation, with IE5.5, NS4.xx, and NS6.xx clients.

    use CGI; use CGI::Carp qw(fatalsToBrowser); sub NiceGetFile { my ($Filename) = @_; my $RawHTML = ""; if (open(FILE,"<".$Filename)) { binmode(FILE); my $Position = sysseek(FILE,0,2); if ($Position) { sysseek(FILE,0,0); sysread(FILE,$RawHTML,$Position); } close(FILE); } return $RawHTML; } # probably should set the CGI config to blast excessive # POSTS here my $DynPage = new CGI; # Do anti-leeching code immediately my $file = 'Demo.zip'; my $ZIPFile = &NiceGetFile('ZIP/'.$file); unless($ZIPFile) { # inform user and email admin # .... }; # NOTE: could use Content-MD5 header here # NOTE: change the type-header to the real MIME-type print $DynPage->header(-type=>'application/zip', -Content_Length=>length($ZIPFile)); binmode( STDOUT ); print $ZIPFile;