I have a nodejs application that calls a server which returns a fragment of an image. I am trying to save this segment in a buffer so that I can later merge them together to make a full image
var req = http.request(options, function(res) {
var segment = parseInt(res.headers["segment"]);
res.setEncoding('binary');
var data = "";
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
images[segment] = (new Buffer(data, 'binary'));
});
});
after I get all the segments
var totalImage = "";
for (image in images) {
totalImage += image.toString('binary');
}
fs.writeFile('image.png', totalImage, function(e) {
console.log('done');
});
I want to use node-pngjs but I am not sure how to stream the response binary into png so that I can save the pixel buffer instead of the binary itself for later consumption
I attempted to do the following:
res.pipe(new PNG()).on('parse', function(err, data) {
buffers[segment] = data;
});
but this lead to an error 'Invalid file signature' during parse