[英]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.