Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

How to Post data to sql and return id then save a base64-encoded image to disk

I want to insert data in sql server then return id , and save base64-encoded image to disk with name (returned Id) this is my code . only insert data in sql server any help to return id , and save base64-encoded image to disk with name (returned Id) this is my code . only insert data in sql server any help to return id , and save base64-encoded image to disk with name (returned Id)

     var express = require('express'); // Web Framework
    var app = express();
    var cors = require('cors');
    var bodyParser = require('body-parser')
    var sql = require('mssql'); // MS Sql Server client
    var fs = require('fs');

    app.use(cors());

    // parse application/x-www-form-urlencoded
    app.use(bodyParser.urlencoded({ extended: false }))

    // parse application/json
    app.use(bodyParser.json())


    const pool = new sql.ConnectionPool({
        user: 'sa',
        password: 'pass',
        server: 'localhost',
        database: 'dbname',
        options: {
        encrypt: false
    }
    })
    ;
    var conn = pool;
    app.post('/api/submitfollow' , function (req, res) {
    var sqlQ = ` insert into tblFollowUpOrb (notificationTypeID,Lat,Long,user_id) values (${req.body.notificationTypeID},${req.body.Lat},${req.body.Long},${req.body.user_id}) , SELECT SCOPE_IDENTITY() AS id `;
    conn.connect().then(function () {
        var req = new sql.Request(conn);
        req.query(sqlQ).then(function (recordset) {

            res.send("1");
            conn.close();   


            var base64Data = req.rawBody.replace(/^data:image\/png;base64,/, "");
fs.writeFile("out.png", base64Data, 'base64', function(err) {
  console.log(err);
});


        })
            .catch(function (err) {
                res.send("0");
                conn.close();
            });
    })
    .catch(function (err) {
        res.send("0");
        });
    })

Comments