Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return binary result from phantomjs webserver

Is there any way to return result froma PhantomJS webserver as binary?

To be more specific, If I render a screenshot of a page as base64, can I then transform this base64 string into binary and return it so the client receives it as an image?

This is what I have so far, I've commented out some of my experiments which apparently doesnt work

response.statusCode = 200;
response.setHeader("Content-Type", "image/png");
//response.setHeader("Content-Encoding","base64");
var base64 = page.renderBase64('png');
//var binary=atob(base64,"b");
response.write(base64  );
response.close();       

Ideas?

like image 553
Roger Johansson Avatar asked Aug 12 '13 07:08

Roger Johansson


1 Answers

You can just set the Encoding to binary, and it will work:

response.statusCode = 200;
response.headers = {
    'Cache': 'no-cache',
    'Content-Type': 'image/png'
};
response.setEncoding('binary');
response.write(atob(page.renderBase64('png')));
response.close();
like image 124
Lux Avatar answered Sep 30 '22 17:09

Lux