使用 node.js 引用多个文件上传到数据库
Referencing multiple files upload to DB with node.js
我不明白如何使用 Node.Js 保存上传到数据库的多个文件的引用(文件名)。
我正在使用 blueimp jquery 文件上传和 multer 中间件执行多个文件上传到本地存储(上传文件夹)。
<input id="fileupload" type="file" name="images" data-url="/record/edit/#{record.id}" multiple>
app.js代码:
//using multer to upload files
const upload_images = upload.fields([{ name: 'featured_img', maxCount: 1 }, { name: 'images', maxCount: 8 }]);
//...
app.post('/record/edit/:id', upload_images, recordController.postUpdate );
recordController.js代码:
exports.postUpdate = ((req, res, next) => {
Record.findById(req.params.id, (err, record) => {
if (req.files ) {
console.log('UPLOADED')
// record.featured_img = req.files['featured_img'][0].filename; //=> working for single file
// Now, how to make it work for multiple files?
console.log(req.files['images'])
record.images = "HOW TO SAVE THE INFO OF REQ.FILES TO DB?";
}
console.log 输出:
UPLOADED
[ { fieldname: 'images',
originalname: 'slippry-02.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
destination: 'C:\Users\agaez\Documents\sanbox\nodeApps\theapp\uploads',
filename: '8mdnuacm1469201049450.jpg',
path: 'C:\Users\agaez\Documents\sanbox\nodeApps\theapp\uploads\8mdnuacm1469201049450.jpg',
size: 49798 } ]
POST /record/edit/578580b43c7a08601730cc06 302 26.654 ms - 82
UPLOADED
[ { fieldname: 'images',
originalname: 'slippry-03.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
destination: 'C:\Users\agaez\Documents\sanbox\nodeApps\theapp\uploads',
filename: 'yrnnzl9h1469201049467.jpg',
path: 'C:\Users\agaez\Documents\sanbox\nodeApps\theapp\uploads\yrnnzl9h1469201049467.jpg',
size: 49792 } ]
更新
我使用 MongoDB 作为数据库,使用猫鼬作为 ODM
更多信息: 目前,我正在使用 jquery 文件上传,当我上传 2 张图片时,它会发出 2 post 请求。如果没有 jquery 文件上传,它会对所有上传的图像发出 1 post 请求,我得到 req.files 一个唯一的数组(所有文件都已上传)。我怎样才能将该信息保存到 MongoDB?
recordModel.js
//...
images: String,
//...
你的 'images' 属性 看起来就像一个单一的字符串值,所以如果你只是在寻找一个逗号分隔的 URL 字符串,最简单的方法就是引入 Lodash 并创建像这样的字符串:
record.images = _.map(req.files['images'], 'path').join(',');
就是说,我认为实际上您不想要一个字符串,而是(我认为)您最好定义一个 ImageSchema,其中包含您从中获取的部分或全部信息req.files
。至少,我会引入路径和 mimeType,但其他信息可能对您有用,我不知道。在任何情况下,如果您定义了 ImageSchema,您可以:
const Image = mongoose.model('Image');
/* your upload stuff happens, and then */
record.images = _.map(req.files['images'], function(i){ return new Image(i);});
这当然假设您将 RecordSchema 更新为:
...
images : [ImageSchema]
...
希望对您有所帮助。
我不明白如何使用 Node.Js 保存上传到数据库的多个文件的引用(文件名)。
我正在使用 blueimp jquery 文件上传和 multer 中间件执行多个文件上传到本地存储(上传文件夹)。
<input id="fileupload" type="file" name="images" data-url="/record/edit/#{record.id}" multiple>
app.js代码:
//using multer to upload files
const upload_images = upload.fields([{ name: 'featured_img', maxCount: 1 }, { name: 'images', maxCount: 8 }]);
//...
app.post('/record/edit/:id', upload_images, recordController.postUpdate );
recordController.js代码:
exports.postUpdate = ((req, res, next) => {
Record.findById(req.params.id, (err, record) => {
if (req.files ) {
console.log('UPLOADED')
// record.featured_img = req.files['featured_img'][0].filename; //=> working for single file
// Now, how to make it work for multiple files?
console.log(req.files['images'])
record.images = "HOW TO SAVE THE INFO OF REQ.FILES TO DB?";
}
console.log 输出:
UPLOADED
[ { fieldname: 'images',
originalname: 'slippry-02.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
destination: 'C:\Users\agaez\Documents\sanbox\nodeApps\theapp\uploads',
filename: '8mdnuacm1469201049450.jpg',
path: 'C:\Users\agaez\Documents\sanbox\nodeApps\theapp\uploads\8mdnuacm1469201049450.jpg',
size: 49798 } ]
POST /record/edit/578580b43c7a08601730cc06 302 26.654 ms - 82
UPLOADED
[ { fieldname: 'images',
originalname: 'slippry-03.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
destination: 'C:\Users\agaez\Documents\sanbox\nodeApps\theapp\uploads',
filename: 'yrnnzl9h1469201049467.jpg',
path: 'C:\Users\agaez\Documents\sanbox\nodeApps\theapp\uploads\yrnnzl9h1469201049467.jpg',
size: 49792 } ]
更新
我使用 MongoDB 作为数据库,使用猫鼬作为 ODM
更多信息: 目前,我正在使用 jquery 文件上传,当我上传 2 张图片时,它会发出 2 post 请求。如果没有 jquery 文件上传,它会对所有上传的图像发出 1 post 请求,我得到 req.files 一个唯一的数组(所有文件都已上传)。我怎样才能将该信息保存到 MongoDB?
recordModel.js
//...
images: String,
//...
你的 'images' 属性 看起来就像一个单一的字符串值,所以如果你只是在寻找一个逗号分隔的 URL 字符串,最简单的方法就是引入 Lodash 并创建像这样的字符串:
record.images = _.map(req.files['images'], 'path').join(',');
就是说,我认为实际上您不想要一个字符串,而是(我认为)您最好定义一个 ImageSchema,其中包含您从中获取的部分或全部信息req.files
。至少,我会引入路径和 mimeType,但其他信息可能对您有用,我不知道。在任何情况下,如果您定义了 ImageSchema,您可以:
const Image = mongoose.model('Image');
/* your upload stuff happens, and then */
record.images = _.map(req.files['images'], function(i){ return new Image(i);});
这当然假设您将 RecordSchema 更新为:
...
images : [ImageSchema]
...
希望对您有所帮助。