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!

In reply to Problems with dynamic images by ibmman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.