使用Multer

时间:2017-04-06 22:31:29

标签: javascript express multer

我尝试使用 Multer 从我的表单上传图片,当我插入图片时一切正常但是当没有选择图片时我收到了错误。

  • 这里是multer配置,所有上传的图像都存储在public / images / uploads中:
//handle file upload

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'public/images/uploads/')
  },
  filename: function (req, file, cb) {
    cb(null, file.fieldname + '-' + Date.now()+ '.jpg')
  }
});
var upload = multer({ storage: storage })

现在我想取回 mainImageName 的值,但是当我没有插入任何图片时,会发生错误cannot get property filename of undefined.

  • 以下是其余的内容:

    //Submit new post
    router.post('/add', upload.single('mainimage'), function(req, res, next) {
    var title = req.body.title;
    var category = req.body.category;
    var body = req.body.body;
    var author = req.body.author;
    var date = new Date();
    
    if(req.file && req.file.mainimage){
    
        var mainImageName = req.file.filename;
        var mainImageMime = req.file.mainimage.mimetype;
        var mainImagePath = req.file.mainimage.path;
        var mainImageSize = req.file.mainimage.size;
    
    }else{
    
        //if there was no image uploaded!!
        //noimage.png should be placed in public/images/uploads
        mainImageName = 'noimage.png';
    }
    
    console.log(mainImageName); //always returns noimage.png !!
    
    //form validation
    req.checkBody('title', 'You forgot the title').notEmpty();
    req.checkBody('body', 'Please fill in the body').notEmpty();
    
    //check errors
    var errors = req.validationErrors();
    
    if(errors){
    

    var mycategories = [];   var categories = db.get(' categories');   categories.find({},{},function(err,categories){

        for (i=0; i<categories.length; i++) {
              mycategories[i] = categories[i];
            }
     });
    res.render('addpost', {
            'pageTitle': 'Add Post',
            'errors': errors,
            'title': title,
            'body': body,
            'categories': mycategories
    
        });
    
    } else{
            var posts = db.get('posts');
            //submit Post
            posts.insert({   "title": title, 
                             "body": body,
                             "category": category,
                             "date": date,
                             "author": author,
                             "mainimage": mainImageName 
    
                     }, function(err, post) {
                if (err){
            res.send('There was an issue submitting the post.')
             } else {
                req.flash('success_msg', 'Post Submitted');
                res.location('/');
                res.redirect('/');
             }
             });
    }
    

    });

1 个答案:

答案 0 :(得分:1)

我发现问题,条件if(req.file && req.file.mainimage)从未得到满足,因为req.file.mainimage始终未定义,因此我只是将其更改为if(req.file)并且运行正常。

这应该是这样的:

if(req.file){

    var mainImageName = req.file.filename;
    var mainImageMime = req.file.mimetype;
    var mainImagePath = req.file.path;
    var mainImageSize = req.file.size;

}else{

    //if there was no image uploaded!!
    //noimage.png should be placed in public/images/uploads
    mainImageName = 'noimage.png';
}