Android
ExpressJS running on AWS
MySQL
在MySQL数据库中
restaurants
ReataurantGalleryImages
我使用相对路径存储图片
我必须将图像存储在以下位置,这是一个公共目录,用于快速存储图像
/public/images
我尝试过的My Express Code ::
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
)
和文件的位置
/MyDirectory/projects/MyProject/public/images
希望我很清楚 。 的由于
答案 0 :(得分:1)
将图像存储在数据库中是不好的做法,因为它会占用数据库的更多资源。相反,您应该尝试将图像存储在磁盘上并将路径存储在表中。
尝试这样的事情:
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();
});
});
);
});
});
然后POST到/ Name,例如http://127.0.0.1/Name
。
实施例
<form name='form' action='/Name' method='post'>
<input type='text' name='name'>
<input type='file' name='image'>
</form