0

I am consuming third party service for downloading images but body of it's response includes html plus base64(not sure) image content on top. Response has content type as image/jpeg; charset=utf-8

Example response:

����JFIF``��C       

 $.' ",#(7),01444'9=82<.342��C          

2!!22222222222222222222222222222222222222222222222222��"��  
���}!1AQa"q2���#B��R��$3br� 
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz���������������������������������������������������������������������������    
���w!1AQaq"2�B����  #3R�br�

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body onload="initslide('method1,method2,method3', '');">
    // More html goes here
</body>
</html>

And service call:

var params = {
  url : serviceUrl,
  form : form,
  headers : headers
};

request.post(params, function(error, response, body) {
  if (error) {
    console.error("Error:", error);
    return;
  }
  callback(body); // In body i am getting above response 
});

Now, I am only interested in downloading image portion of it and save it on cloud as png/jpeg format. Any idea how to achieve this in node.js.

2
  • Can I ask what service you are using? I may help me formulate an answer. what needs to be done is separate that image data from the HTML. We just have to figure out a scheme of knowing where the image ends and the HTML begins.
    – powerc9000
    Apr 6, 2017 at 15:48
  • @powerc9000 Well, I can't disclose the service as it's proprietary third party service. Apr 6, 2017 at 15:53

1 Answer 1

0

I think we can make the assumption that the image won't contain the sequence \n\n<! so let's split on that.

var parts = body.split('\n\n<!')

Now the first part should contain our image

var imageData = new Buffer(parts[0], 'binary');

So then we can save this to a file

//I'm just assuming jpg for now
fs.writeFile('out.jpg', imageData, cb);

More can be done to interpret the file type and make it more robust but this is the basic idea behind it.

Alternatively you can read the entire file into a buffer and look for the end of image sequence in the JPEG (FFD9) which will tell you when the image begins and the html ends.

var data = new Buffer(body, 'binary');
for(var i=0; i<data.length; i++){
    if(data[i] === 0xff && data[i+1] === 0xd9){
        //end of file
2
  • Thanks @powerc9000, I will try this. One question when I use REST client to test service it extract image and shows that in image tab section. How that works? Apr 7, 2017 at 8:08
  • @user3640709 There's not a lot to go off so I have a hard time really helping. If it were at all possible to save one of the HTML responses and send it to me I might be able to do more for you.
    – powerc9000
    Apr 7, 2017 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.