Question

I used perl LWP module to fetch an image from a URL.

CGI FILE(say img.cgi)

my $ua = LWP::UserAgent->new( timeout => $timeout );
my $respons = $ua->request(POST $url, Authorization => $auth_header);
if ($respons->is_success) {
  $ImgContent = $respons->decoded_content;
}
print $ImgContent;

Then I am calling above CGI as an http request, using cold fusion:

img_display.cfm file

<cf_http method="POST" url="img.cgi" timeout="60" throwonerror="no" local="yes">
....
</cf_http>
<cfoutput>
   #cfhttp.filecontent#
</cfoutput>

Whenever I hit the URL ......./img_display.cfm, from internet explorer browser, the browser displays the image perfectly.

However whenever I hit the same URL from Chrome or FireFox, instead of image, browser display, some weird characters(equivalent to the image opened in note pad).

ÿØÿàJFIFSÿþ4Created using the Colosseum Builders JPEG libraryÿÀ&ŠÿÛ„  " "  ÿÄ{  !"t³Ô#1468UWv–²´Ó27AQVu”•$5BXfs“¦Ñ%3DRTb‘±aµ&qr†—¡Ò CGw‚„ÃÿÚ?úF4cM®ž¦Ý=sÓÜJléXÕ½é±F'p£6¤dJ‘*ªªª÷ª­oÁ:Yói†}^‹øtö ÒÏ›L3êô_ç°N–|ÚaŸW¢þ=‚t³æÓú½ðé쥟6˜gÕ过O`,ù´Ã>¯Eü:{égͦõz/áÓØ'K>m0Ï«ÑžÁ:Yói†}^‹øtö 

I have no idea, why IE displays image while chrome and Firefox displays image code.

Any help is appreciated.

Was it helpful?

Solution

What you are seeing is the string representation of the binary image data. The browser is displaying that because it does not know it's an image.

You need to print the MIME-type of the image in the HTTP header and set your output to binmode.

my $q = CGI->new;
print $q->header('image/jpeg');
binmode STDOUT;
print $ImgContent;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top