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

Hello everyone I'm working on a pretty big-ish web app for interfacing with a multimedia database, and I'm using CGI::Application, along with ImageMagick to deal with converting images, generating resized preview files, etc.

I have a feeling I'm not doing something right with returning my images from the runmode which fetches the filedata. Everything works great when I embed it in an <IMG> tag, but if I try to make a download link in an <A HREF> the binary data is returned. I know that's fairly characteristic of improper headers, but I'm calling $q->header( -type => 'image/jpeg'); before reading the file data from the file to a buffer variable and then returning that data.

Kind of a separate but related issue is that I can't get the browser to display a filename/file extension with doing 'save (link) as' on the download link, or 'save image as' on the image file.

I had found an article with some examples using a redirect to append a "/image.extension" onto the URL, but that might be old and probably doesn't work anymore (and seems like it was kind of a hack anyway). I know this is a common situation, but I haven't been able to find much of any information on it.

This is what I have now:
sub getMediaFile{ my $self = shift(); my $q = $self->query(); my $fileId = $q->param("fileId"); my $getPreviewFile = $q->param("preview"); my $mediaFileInfoTemp = BioMeRSAModel::getMediaInfo($fileId); my %mediaFileInfo = %$mediaFileInfoTemp; my $filePath = $mediaFileInfo{"path"}."/".$mediaFileInfo{"filename +"}; if ($getPreviewFile eq "true"){ my $NameWithoutExtension = $mediaFileInfo{"filename"}; $NameWithoutExtension =~ s/\.[^.]*$//; my $previewFilePath = $mediaFileInfo{"path"}."/".$NameWithoutE +xtension."_preview.jpg"; if (-e $previewFilePath){ $filePath = $previewFilePath; } } $q->header( -type => 'image/jpeg'); my $fileData; my $fileBuffer; open MEDIAFILE, $filePath or return "error opening file"; binmode MEDIAFILE; while(read(MEDIAFILE, $fileBuffer, 1)){ $fileData.=$fileBuffer; } return $fileData;
Thanks!

Replies are listed 'Best First'.
Re: Problems with dynamic images
by Anonymous Monk on Mar 08, 2009 at 08:29 UTC
    but I'm calling $q->header( -type => 'image/jpeg'); before reading the file data from the file to a buffer variable and then returning that data.

    perldoc CGI shows that you have to print the header,

    $ perl -MCGI -le"print CGI->header(qw! -type image/jpeg -nph 1!) HTTP/1.0 200 OK Server: cmdline Date: Sun, 08 Mar 2009 08:14:05 GMT Content-Type: image/jpeg
    What you probably want is from CGI::Application
    # add or replace the 'type' header $webapp->header_add( -type => 'image/jpeg', -attachment => 'foo.jp +g' );

    I had found an article with some examples using a redirect to append a "/image.extension" onto the URL, but that might be old and probably doesn't work anymore (and seems like it was kind of a hack anyway)
    It was probably a hack for browsers which ignored attachment headers.

    See also overkill anti-caching CGI headers

      Thanks! Exactly what I was looking for. I figured that changing the header by manipulating the query object would have accomplished that, but apparently not. Sometimes it's sort of tough to tell what sorts of base CGI manipulations end up affecting results in CGI::Application.