perl: correct `content type` formatting for return

2019-03-04 12:55发布

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: enter image description here enter image description here But the call is executed: enter image description here

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:

enter image description here

Any ideas on problem/fix ?

1条回答
何必那么认真
2楼-- · 2019-03-04 13:17

Perl subroutine:

my $photo = '/9j/4gIcSUNDX....oqOU1MzEFUa8x2MJf5JUjFV3llWW3UVscTLNhd93KFWQUrqHn/ABEc2CfmIASx0nMWLiN5f2hqE//Z';
my $decoded= MIME::Base64::decode_base64($photo);
content_type 'image/jpeg';
$decoded;
查看更多
登录 后发表回答