【问题标题】:Save image data with Mongoose使用 Mongoose 保存图像数据
【发布时间】:2015-01-23 23:18:26
【问题描述】:

我正在尝试将图片上传到我的 mongoDB 作为事件创建的一部分。这是我目前所拥有的:

事件架构:

var mongoose = require('mongoose');
var Event = new mongoose.Schema({
    author: mongoose.Schema.Types.ObjectId,
    authorName: String,
    name: String,
    priority: Number,
    status: String,
    date: Date,
    img: { data: Buffer, contentType: String },
    description: String,
    joinedUsers: [{ 
        username: {
            type: String, 
            required: true, 
            index: { unique: true }
        }, 
        petitionStatus: String 
    }]
});

module.exports = mongoose.model('Event', Event);

我的玉观:

form.form-signin(enctype="multipart/form-data",role='form',method='POST',action='/newEvent')
  h2.form-signin-heading Create an Event
  input.form-control(name='name', placeholder='Name of Event', required='', autofocus='')
  textarea.form-control.event-description(placeholder='Please Enter a Description', name='description', required='')
  input.form-control(name='date', type='date', required='')
  input.form-control(name='img', type='file', required='')
  button.btn.btn-lg.btn-primary.btn-block(type='submit') Create

快速 API 路由:

app.post('/newEvent',routes.createEvent);

我的保存方法

functions.createEvent = function(req, res){
    var author; 
    var name = req.param('name');
    var description = req.param('description');
    var date = req.param('date');

    UserSchema.findOne({ username: req.session.passport.user }, function(err, user) {
        if (err) {
            console.log(err);
            res.status(404).json({status: err})
        }
        if (user) {
            author = user.getID();
            console.log(author);

            var record = new EventSchema({
                name: name,
                description: description,
                date:date,
                author:author,
                authorName: req.session.passport.user
            });

            record.save(function(err) {
                if (err) {
                    console.log(err);
                    res.status(500).json({status: err});
                } else {
                    res.redirect('/userEvents');
                }
            });
        }
    });
};

我有 express 4,所以不推荐使用 bodyparser。如果有人能告诉我实现图片上传的好方法,我将不胜感激。

提前致谢

【问题讨论】:

    标签: jquery node.js mongodb express mongoose


    【解决方案1】:

    body 解析器在 express 4 中没有被弃用,但是你可以使用 busboy,你只需要配置你的应用程序

    app.use(busboy());
    

    然后图像将可以从请求中访问,例如

     exports.uploadDocument = function(req,res){
        var fstream;
        req.pipe(req.busboy);
        var doc = new DocumentModel(); // just a example use your own
    
        req.busboy.on('file',function(fieldname, file, filename, encoding, contentType){
            doc.contentType = contentType;
            doc.encoding= encoding;
            doc.filename= filename;
            file.on('data',function(data){
                doc.content = data;
                doc.size = data.length;
            });
        });
    

    【讨论】:

      猜你喜欢
      • 2017-06-30
      • 1970-01-01
      • 2020-09-13
      • 1970-01-01
      • 1970-01-01
      • 2017-07-31
      • 1970-01-01
      • 2020-08-10
      • 2015-11-13
      相关资源
      最近更新 更多