I am accessing third party database where there are images stored in varbinary column. I am writing node.js application to restore varbinary images stored in MS Sql server into
Resolved It was difficult until I studied how Encoding/Decoding and varbinary works.
Solution : Images were stored as hexadecimal numbers representation of base64 encoded string in Sql server as data type varbinary. While fetching record node.js mssql library converts the hexadecimal numbers into javascript buffer(This buffer is of base64 encoded string and not actual image). Then I converted this buffer back to base64 encoded string of image like..
var originalBase64ImageStr = new Buffer(resultSet[0].Image).toString('utf8');
Then created converted back to actual image buffer like..
var decodedImage = new Buffer(originalBase64ImageStr , 'base64')
fs.writeFile(__dirname+'/../public/images/img3.jpg', decodedImage, function(err, data){
if (err) throw err;
console.log('It\'s saved!');
cb(data);
});
Note: Node.js mssql library works differently for string representation of varbinary(returned buffer is representation of original string so no need to follow above steps) and image/doc representation of varbinary(returned buffer is representation of base64 encoded string and need to follow above steps).