【问题标题】:can't get file property using Multer无法使用 Multer 获取文件属性
【发布时间】:2017-09-02 04:03:01
【问题描述】:

我正在尝试使用 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 })

现在我想取回 ma​​inImageName 的值,但是当我不插入任何图像时会出现错误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('/');
             }
             });
    }
    

    });

【问题讨论】:

  • 那是……对。如果您不传递文件,则req.files 将为空,因此req.files[0] 将为undefined,因此req.files[0].filename 将导致错误(因为您试图获取undefined 对象的属性)。在第一个 IF 语句中移动有问题的console.log(),这样可以防止错误发生。
  • 谢谢Mikey 但我不明白我是否传递了文件 mainImageName 总是返回 noimage.png。
  • 使用您的路线定义编辑您的问题,例如app.post('whatever', upload...
  • 那是因为您在代码中将其设置为“noimage.png”:var mainImageName = 'noimage.png'
  • @fredrover 我想他是想说当他有图像时它永远不会进入 IF 块。

标签: javascript express multer


【解决方案1】:

我发现了问题,条件 if(req.file &amp;&amp; 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';
}

【讨论】:

    猜你喜欢
    • 2019-12-24
    • 2021-03-29
    • 1970-01-01
    • 2022-12-07
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    • 2023-03-24
    相关资源
    最近更新 更多