Monday, 2 October 2017

Saving image from SQL Server Database

hello,i have a connection up from node.js to sql server database and i want to retrieve an image present in the database and save it localy. Here is what i found on the web but unfortunaly when i try to open the saved image it says we can't open this file.Code:function SaveImage() { var dbConn = new sql.ConnectionPool(config); dbConn.connect().then(function () { var request = new sql.Request(dbConn); request.query("select Image from images where ImageId=1").then(function (recordsets) { console.log(recordsets.recordset[0].Image); var decodedImage = new Buffer(recordsets.recordset[0].Image).toString('base64'); fs.writeFile(__dirname + "/img3.jpg", decodedImage,'hex', function(err, data){ if(err){ console.log("Decoder Error: ", err); }else{ console.log("Saved"); } }); dbConn.close(); }).catch(function (err) { console.log(err); dbConn.close(); }); }).catch(function (err) { console.log(err); }); }

Submitted October 02, 2017 at 10:25AM by mikelak

No comments:

Post a Comment