Return binary result from phantomjs web server

Is there a way to return the result from the PhantomJS web server as binary?

To be more specific, if I create a screenshot of the page as base64, can I convert this base64 string to a binary file and return it so that the client accepts it as an image?

This is what I have so far; I have commented on some of my experiments, which apparently do not 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?

+6
source share
2 answers

You can simply set the encoding to a binary file 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(); 
+6
source

The solution is to use binary coding in the web server module and . evaluate inside the web page module to create binary content.

the result looks something like this (assuming the page is defined):

 response.statusCode = 200; response.setEncoding("binary"); response.setHeader("Content-Type", "image/png"); var base64 = page.renderBase64('png'); var binary = page.evaluate(function (data) { return atob(data, "b");}, base64); response.write(binary) response.close(); 
0
source

Source: https://habr.com/ru/post/951535/


All Articles