Pergunta

I am new to programming and I am trying to store images which i recieve as POST request from my Client to my server


  1. Client:: Android
  2. Server:: ExpressJS running on AWS
  3. Database i am using :: MySQL

In MySQL DATABASE

  1. I have created a Database in mySQL:: restaurants
  2. There is a table named:: ReataurantGalleryImages

I am using relative path to store the images


I have to store the images in the place as below which is a public directory to store images in express

/public/images

My Express Code that i have tried::

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: '**********'
});

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 = new Buffer(request.query.Key, 'base64').toString('binary');
   var keyImage = new Buffer(request.query.Key2, 'base64').toString('binary');
   var name_of_restaurants;
   async.series( [

       function(callback) {

          connection.query('INSERT INTO RestaurantGalleryImages (Images,RestName) VALUES (?,?)', [keyImage,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'));
});

My Queries::

  • Am i correct with my code
  • How it the name gets stored in table for the varchar field (Images) and file for the location /MyDirectory/projects/MyProject/public/images
  • How mapping of whole thing takes place

Hope i am clear . Thanks

Foi útil?

Solução

Storing images in the database is bad practice, as it consumes more resources of your database. Rather, you should try storing images on disk and store the path in the table.

Try something like this:

    app.post('/upload'),function(req,res){
       req.form.complete(function(err,fields,files){
          async.series([
             function(cb){
                fs.rename(files.image.path+files.image.name,'./public/image/'+files.image.name,function(err){

                connection.query('INSERT INTO RestaurantGalleryImages (Images,RestName) VALUES (?,?)', [fields.name,files.image.path+'/'+files.filename],
                   function(err){
                      cb();
                 });
                 });
          );
        });
    });

You'd then POST to /Name, for example http://127.0.0.1/Name. Ex.

<form name='form' action='/Name' method='post'>
   <input type='text' name='name'>
   <input type='file' name='image'>
</form
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top