Pergunta

How to decode the param values received which were received as Base64 encoded form and insert into database ?

This is what i have tried.

  • According to this i am getting one value recieved from the client as param value and inserting into server ( I have recieved the request at POST )
  • No base64 encoding is done here

I am using this code at present :

var express = require('express')
 , async = require('async')
 , http = require('http')
 , mysql = require('mysql');

var app = express();

var connection = mysql.createConnection({
   host: 'localhost',
   user: '******',
   password: "******",
   database: 'posting_information_DB'
});

connection.connect();

// all environments
app.set('port', process.env.PORT || 1234);

app.use(express.static(__dirname + '/public/images'));


app.post('/Name/',function(request,response,next){


app.use(express.bodyParser());

   var keyName=request.query.Key;
   var name_of_restaurants;
   async.series( [

       function(callback) {

          connection.query('INSERT INTO details (name) VALUES (?)', [keyName], function (err, rows, fields) 
              {
                      console.log('Connection result error ' + err);        
                      callback();
              });
       }

  // Send the response
] );
} );


http.createServer(app).listen(app.get('port'), function(){
 console.log('Express server listening on port ' + app.get('port'));
});

What i am trying to do !

  • Now what changes should i need to make so that when i need recieve a image and string as two param values
  • These values are Base64 encoded
  • How to decode these Base64 here and then insert the retrieved param values to database

How to modify my posted Express code !

Thanks !

Foi útil?

Solução

You can retrieve the image parameter using request.params and then create a Buffer object, specify the base64 encoding and then convert it using the .toString() method.

app.post('/Name/', function(request, response, next){
  var image = new Buffer(request.params.image, 'base64').toString('binary');
  // do the database insert...
});
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top