Outputting binary data
Hi all, If I've got a scalar $data containing some binary data (a JPG image read using read_file($file, binmode => ':raw' ), how can I cause one of my routes to output it? I've done essentially this (simplified): get '/some/route' => sub { my $data = …get_image…; header 'Content-Type' => 'image/jpeg'; return $data; } but this doesn't feel right and causes a broken output (appears to return only a few rows of pixels of the image). Any tips? Cheers -- Rik Brown http://www.rikbrown.co.uk
I'll follow myself up here almost immediately by saying I fixed this - it was my own fault reading the data in the first place. However, if anyone has any thoughts whether what I've done conforms to any "Right Way" of outputting binary data via Dancer, please let me know. Thanks! -- Rik Brown http://www.rikbrown.co.uk On Tuesday, 1 January 2013 at 18:17, Rik Brown wrote:
Hi all,
If I've got a scalar $data containing some binary data (a JPG image read using read_file($file, binmode => ':raw' ), how can I cause one of my routes to output it?
I've done essentially this (simplified):
get '/some/route' => sub { my $data = …get_image…;
header 'Content-Type' => 'image/jpeg'; return $data; }
but this doesn't feel right and causes a broken output (appears to return only a few rows of pixels of the image).
Any tips?
Cheers
-- Rik Brown http://www.rikbrown.co.uk
On Tue, 1 Jan 2013 18:17:28 +0000 Rik Brown <rik@rikbrown.co.uk> wrote:
Hi all,
If I've got a scalar $data containing some binary data (a JPG image read using read_file($file, binmode => ':raw' ), how can I cause one of my routes to output it?
I've done essentially this (simplified):
get '/some/route' => sub { my $data = …get_image…;
header 'Content-Type' => 'image/jpeg'; return $data; }
but this doesn't feel right and causes a broken output (appears to return only a few rows of pixels of the image).
That's acceptable enough, and should work (and, according to your subsequent reply, does after fixing some problem with reading the image). If it's a simple as sending the content of an image file, you probably want to just let send_file() do it. e.g. send_file($filename, content_type => "image/jpeg"); You can omit the content_type option if you want to, in which case a reasonable guess will be made for you based on the filename. The send_file() keyword also understands a scalar ref as content to send, so you could equally say: send_file(\$image_data, content_type => "image/jpeg", filename => "myimage.jpg", ); -- David Precious ("bigpresh") <davidp@preshweb.co.uk> http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter www.preshweb.co.uk/linkedin www.preshweb.co.uk/facebook www.preshweb.co.uk/cpan www.preshweb.co.uk/github
participants (2)
-
David Precious -
Rik Brown