user6865700
user6865700

Reputation:

Check if image is submitted or not using node.js and multer

I am able to upload images but if i need to edit my page where sometimes I need not upload a newer image, how do I proceed?

I am checking using the below code but every time its giving me "undefined".

    router.post('/update/:id', function(req, res, next) {

    var Storage = multer.diskStorage({
        destination: function (req, file, callback) {
            callback(null, "./uploads/images");
        },
        filename: function (request, file, callback) {
            callback(null, file.originalname);
        }
      });

    upload(req, res, function (err) {
      if (typeof req.files.image !== "undefined") {
        // code
      } else if (typeof req.files.image === "undefined") {
        // code
      }

    home.update(req.files[0].path, req.body.input_field_name, req.params.id);
      req.flash('edit', 'Updated.');
      res.redirect('edit/' + req.params.id);
    });

    });

Any help?

Solution

upload(req, res, function (err) {
  if (typeof req.files !== 'undefined' && req.files.length > 0) {
      //code
  } else {
      //code
  }
});

Upvotes: 0

Views: 3747

Answers (1)

Manvendra Priyadarshi
Manvendra Priyadarshi

Reputation: 255

   if (typeof req.query.image !== "undefined") {
        // code
        console.log("File is uploaded");
      } else if (typeof req.query.image === "undefined") {
        // code
        console.log("File is not uploaded");
      }

//if you are using post then use - req.body.image

Upvotes: 1

Related Questions