节点restful api, post之前的文件上传并保存在数据库中的文件名
Node restful api, File upload and save file names in database before post
我开发了一个节点restfulapi。我需要上传一些图像文件并将文件名保存到数据库中。怎么做。这是我的代码
var express = require('express');
var multer = require('multer')
var upload = multer({ dest: 'uploads/' })
var router = express.Router();
var path = require('path');
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, './uploads');
},
filename: function (req, file, callback)
{
callback(null, Date.now('nano') + path.extname(file.originalname));
}
});
var upload = multer({ storage : storage }).array('userPhoto',2);
// Models
var Car = require('../models/car');
// Routes
Car.methods(['get','put','post','delete']);
// before file uplod part
Car.before('post',function(req,res,next)
{
var car_photos = [];
upload(req,res,function(err)
{
if(err)
{
return res.end("Error uploading file.");
}
else
{
for(var i = 0; i < req.files.length; i++)
{
car_photos.push(req.files[i]['filename']);
}
}
});
req.body.car_photos = car_photos;
next();
});
Car.register(router,'/car');
module.exports = router;
此 car_photos 数组为空。我如何将这个数组保存到我的数据库中。请帮助。
Car.before('post',function(req,res,next)
{
var car_photos = [];
upload(req,res,function(err)
{
if(err)
{
return res.end("Error uploading file.");
}
else
{
for(var i = 0; i < req.files.length; i++)
{
car_photos.push(req.files[i]['filename']);
}
}
});
req.body.car_photos = car_photos;
next();
});
req.body.car_photos = car_photos;
下一个();
这些行应该在上传函数回调中。在 for 循环之后的 else 块中。
此示例适用于任何使用库节点的人-restful
https://github.com/baugarten/node-restful
为模型添加一个字段:
car_photos: Array
var express = require('express');
var multer = require('multer')
var upload = multer({ dest: 'uploads/' })
var router = express.Router();
var path = require('path');
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, './uploads');
},
filename: function (req, file, callback)
{
callback(null, Date.now() + path.extname(file.originalname));
}
});
var upload = multer({ storage : storage }).array('car_photos',2);
// Models
var Car = require('../models/car');
// Routes
Car.methods(['get','put','post','delete']);
// before file uplod part
Car.before('post',function(req,res,next)
{
var car_photos = [];
upload(req,res,function(err)
{
if(err)
{
return res.end("Error uploading file.");
}
else
{
for(var i = 0; i < req.files.length; i++)
{
car_photos.push(req.files[i]['filename']);
}
req.body.car_photos = car_photos;
next();
}
});
});
Car.register(router,'/car');
module.exports = router;
我开发了一个节点restfulapi。我需要上传一些图像文件并将文件名保存到数据库中。怎么做。这是我的代码
var express = require('express');
var multer = require('multer')
var upload = multer({ dest: 'uploads/' })
var router = express.Router();
var path = require('path');
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, './uploads');
},
filename: function (req, file, callback)
{
callback(null, Date.now('nano') + path.extname(file.originalname));
}
});
var upload = multer({ storage : storage }).array('userPhoto',2);
// Models
var Car = require('../models/car');
// Routes
Car.methods(['get','put','post','delete']);
// before file uplod part
Car.before('post',function(req,res,next)
{
var car_photos = [];
upload(req,res,function(err)
{
if(err)
{
return res.end("Error uploading file.");
}
else
{
for(var i = 0; i < req.files.length; i++)
{
car_photos.push(req.files[i]['filename']);
}
}
});
req.body.car_photos = car_photos;
next();
});
Car.register(router,'/car');
module.exports = router;
此 car_photos 数组为空。我如何将这个数组保存到我的数据库中。请帮助。
Car.before('post',function(req,res,next)
{
var car_photos = [];
upload(req,res,function(err)
{
if(err)
{
return res.end("Error uploading file.");
}
else
{
for(var i = 0; i < req.files.length; i++)
{
car_photos.push(req.files[i]['filename']);
}
}
});
req.body.car_photos = car_photos;
next();
});
req.body.car_photos = car_photos; 下一个();
这些行应该在上传函数回调中。在 for 循环之后的 else 块中。
此示例适用于任何使用库节点的人-restful
https://github.com/baugarten/node-restful
为模型添加一个字段:
car_photos: Array
var express = require('express');
var multer = require('multer')
var upload = multer({ dest: 'uploads/' })
var router = express.Router();
var path = require('path');
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, './uploads');
},
filename: function (req, file, callback)
{
callback(null, Date.now() + path.extname(file.originalname));
}
});
var upload = multer({ storage : storage }).array('car_photos',2);
// Models
var Car = require('../models/car');
// Routes
Car.methods(['get','put','post','delete']);
// before file uplod part
Car.before('post',function(req,res,next)
{
var car_photos = [];
upload(req,res,function(err)
{
if(err)
{
return res.end("Error uploading file.");
}
else
{
for(var i = 0; i < req.files.length; i++)
{
car_photos.push(req.files[i]['filename']);
}
req.body.car_photos = car_photos;
next();
}
});
});
Car.register(router,'/car');
module.exports = router;