Android
ExpressJS running on AWS
MySQL
In MySQL DATABASE
restaurants
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'));
});
Images
)
and file for the location
/MyDirectory/projects/MyProject/public/images
Hope i am clear . Thanks
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