Multer nodejs - req.file 未定义

Multer nodejs - req.file is undefined

我正在使用 Node、Express 和 ejs 以及 multer 创建一个用于上传图像的应用程序。每次我提交表单时,req.file 都是未定义的。我花了一整天的时间进行故障排除,但无法弄清楚我做错了什么。

HTML

<form action="/post" id="formPost" method="post" enctype="multipart/form-data">
     <input class="img-file" type="file" name="image" required>
     <input class="submit" type="submit" value="Post" />
</form>

app.js

const path = require('path');
const express = require('express');
const morgan = require('morgan');
const bodyParser = require("body-parser");
const multer = require('multer');
const app = express();

app.use(express.static(path.join(__dirname, 'public')))
app.set('view engine', 'ejs'); 
app.set('views', path.join(__dirname,'resources/views'));
app.use(bodyParser.json());   
app.use(bodyParser.urlencoded({ extended: true }));

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, "./image");
  },
  filename: function (req, file, cb) {
    console.log(req.file);

    cb(null, Date.now() + "-" + file.fieldname + ".png");
  },
});
const upload = multer({ storage: storage });
app.post("/post", upload.single("image"), (req, res) => {
      console.log(req.file);
});
app.get("/post", (req, res) => {
  res.render("post");
});
app.listen(, () => {
  console.log(`Example app listening at http://localhost:3000/login`);
});

你有一些小错误:首先你忘记添加 port 而不是 login 而应该是 post 然后我们立即找到正确的地址,避免错误 Cannot GET /login

app.listen(3000, () => {
  console.log(`Example app listening at http://localhost:3000/post`);
});

项目文件夹和文件结构:


app.js 我添加了一个简单的错误处理程序到:

app.post("/post", upload.single("image"), (req, res, next) => {}

const path = require("path");
const express = require("express");
const morgan = require("morgan");
const bodyParser = require("body-parser");
const multer = require("multer");
const app = express();

app.use(express.static(path.join(__dirname, "public")));
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "resources/views"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, "./image");
  },
  filename: function (req, file, cb) {
    console.log(req.file);

    cb(null, Date.now() + "-" + file.fieldname + ".png");
  },
});
const upload = multer({ storage: storage });

// app.post("/post", upload.single("image"), (req, res) => {
//   console.log(req.file);
// });

app.post("/post", upload.single("image"), (req, res, next) => {
  const file = req.file;
  if (!file) {
    const error = new Error("Please upload a file");
    error.httpStatusCode = 400;
    return next(error);
  }
  res.send(file);
  console.log("Success", req.file);
});

app.get("/post", (req, res) => {
  res.render("post");
});

app.listen(3000, () => {
  console.log(`Example app listening at http://localhost:3000/post`);
});

post.ejs

<form action="/post" id="formPost" method="post" enctype="multipart/form-data">
  <input class="img-file" type="file" name="image" required />
  <input class="submit" type="submit" value="Upload File" />
</form>

输出:

选择文件并按下上传文件后:

VSCode 输出:

很有魅力;-)