简体   繁体   中英

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
  }
});
   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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM