简体   繁体   English

如何在控制台上查看我上传的图片名称?

[英]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:我对使用 express 和 NodeJs 很陌生,我创建了一个名为 upload 的 function 上传图片,代码如下:

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:我试图在 POST 发生时在控制台上查看文件名,POST 代码如下:

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);我正在尝试做这样的事情: console.log(req.filename);

You are looking for req.files.FileFieldName where FileFieldName is name attribute of your file element in your html form.您正在寻找req.files.FileFieldName ,其中 FileFieldName 是 html 表单中文件元素的名称属性。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在浏览器控制台中看不到 react lint 错误 - I can't see react lint errors in my browser console 为什么我在 Chrome 控制台中看不到我的对象反应? - Why I can't see my objects in chrome console in react? "如何查看 console.log() 的输出?" - How can I see the output of console.log()? 如何在 console.log 中看到 function 的 output? - How can I see the output of a function in a console.log? 如何在 Firefox 中通过控制台查看 HTML DOM 结构? - How can I see HTML DOM structure via console in Firefox? Blob 图像不会显示在 UI 中,尽管我可以在控制台上看到它们 - Blob images does not show up at UI, although I can see them on console 我得到一个JSON / Javascript对象是因为可以在控制台中看到它,但是如何获取它进行序列化并使数据绑定到Ui元素呢? - I am getting a JSON/Javascript object because I can see it in my console, but how do I get it to serialize and make my data bind to my Ui elements? 我在使用 ckeditor 上传图片时遇到问题 - I have a problem with uploading my images with ckeditor 如何在我的组件上看到我的数组? / 反应 / Javascript - How can i see my array on my component ? / React / Javascript 我看不到我的CreateJS - I can not see my CreateJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM