图像文件名未使用Multer插入db中

时间:2015-12-16 21:14:44

标签: node.js express multer

我尝试使用教程中指定的multer。然后意识到它现在不像过去那样以相同的方式工作。所以我检查了multer的github页面并按照文档进行操作。但是,我的图像虽然上传了,但文件名并没有存储在数据库中。

这是我的代码:

var express = require('express');
var router = express.Router();
var multer = require('multer');
var upload = multer({dest: './public/images/uploads/'});

router.post('/add', upload.single('mainimage'), function(req, res, next) {
    //get form values
    var title = req.body.title;
    var category = req.body.category;
    var body = req.body.body;
    var author = req.body.author;
    var date = new Date();
    var mainImageName;



    if(req.files && req.files.mainimage){

        var mainImageNameOriginalName = req.files.mainimage.originalname;
        mainImageName = req.files.mainimage.name;
        var mainImageMime = req.files.mainimage.mimetype;
        var mainImagePath = req.files.mainimage.path;
        var mainImageExtension = req.files.mainimage.extension;
        var mainImageSize = req.files.mainimage.size;

    }else{
      mainImageName = 'noimage.png';
    }

    // db insertion and yadi yadi yada

});

module.exports = router;

虽然图像上传了,但存储的图像文件名为noimage.png。我在顶部定义了mainImageName以避免提升

1 个答案:

答案 0 :(得分:0)

1)查看req.file

中的单个文件

2)确保您的html表单具有属性enctype="multipart/form-data"