1

I tried using multer as was specified in a tutorial. Then realized that it does not work in the same way now as it was used to be. So I checked the github page of multer and followed the documentation. However, my image though getting uploaded, the filename is not getting stored in the database.

Here is my code :

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;

The image filename that is getting stored is noimage.png though the image is getting uploaded. I defined the mainImageName on the top to avoid hoisting

marukobotto
  • 759
  • 3
  • 12
  • 26

1 Answers1

0

1) Look single file in req.file

2) Make sure your html-form has an attribute enctype="multipart/form-data"

stdob--
  • 28,222
  • 5
  • 58
  • 73
  • If the form didn't have that attribute, then the image wouldn't have been uploaded I think. I checked doing `req.file`. Same issue – marukobotto Dec 16 '15 at 21:55