perl: correct `content type` formatting for returning `image data uri`

十年热恋 提交于 2019-12-04 07:08:27

问题


I have a template in angularjs expecting the image data uri returned by a server call invoked through the src attribute of the img element of the template:

<img width="200px" height="200px" src="http://localhost:3000/returnimage" />

On the server side, written in Perl with Dancer I have:

any ['get', 'post'] => '/returnimage' => sub {
    content_type 'image/jpeg;base64;';
    'data:image/jpeg;base64,/9j/4gIcSUNDX1B....N5f2hqE//Z';
};

The result on template render is a broken image tag: But the call is executed:

The network response and preview are unavailable. But if I call http://localhost:3000/returnimage directly from the browser they do contain the complete image data uri as 'data:image/jpeg;base64,/9j/4gIcSUNDX1B....N5f2hqE//Z'. ALthough a black image shows up and when inspected:

Any ideas on problem/fix ?


回答1:


Perl subroutine:

my $photo = '/9j/4gIcSUNDX....oqOU1MzEFUa8x2MJf5JUjFV3llWW3UVscTLNhd93KFWQUrqHn/ABEc2CfmIASx0nMWLiN5f2hqE//Z';
my $decoded= MIME::Base64::decode_base64($photo);
content_type 'image/jpeg';
$decoded;


来源:https://stackoverflow.com/questions/46565373/perl-correct-content-type-formatting-for-returning-image-data-uri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!