Omran Al_haddad
Omran Al_haddad

Reputation: 117

How I can see my uploading images' names on console?

I am pretty new on using express and NodeJs, I have created a function named upload which upload images, the code as follow:

const fs = require("fs");
var UserId = 2;
var storage = multer.diskStorage({
  destination: function (req, file, callback) {
    var dir = "./public/images/uploads/";
    if (!fs.existsSync(dir)) {
      fs.mkdirSync(dir);
    }
    dir = "./public/images/uploads/" + UserId;
    if (!fs.existsSync(dir)) {
      fs.mkdirSync(dir);
    }
    callback(null, dir);
  },
  filename: function (req, file, cb) {
    cb(null, file.fieldname + "-" + Date.now() + ".jpg");
  },
});

const maxSize = 1 * 1000 * 1000;

var upload = multer({
  storage: storage,
  limits: { fileSize: maxSize },
  fileFilter: function (req, file, cb) {
    // Set the filetypes, it is optional
    var filetypes = /jpeg|jpg|png/;
    var mimetype = filetypes.test(file.mimetype);

    var extname = filetypes.test(path.extname(file.originalname).toLowerCase());

    if (mimetype && extname) {
      return cb(null, true);
    }

    cb(
      "Error: File upload only supports the " +
        "following filetypes - " +
        filetypes
    );
  },

}).array("mypic", 2);

I am trying to see the filename on console when POST happen, the POST code as follow:

app.post("/uploadProfilePicture", function (req, res, next) {
  upload(req, res, function (err) {
    if (err) {
      res.send(err);
    } else {
      res.send("Success, Image uploaded!");
    }
  });
});

I am trying to do something like that: console.log(req.filename);

Upvotes: 0

Views: 48

Answers (1)

Rameez
Rameez

Reputation: 1712

You are looking for req.files.FileFieldName where FileFieldName is name attribute of your file element in your html form.

if(req.files)
{
    console.log(req.files.FormFieldName);
}

Upvotes: 1

Related Questions