通过 multer 上传时无法访问文件

问题描述 投票:0回答:0

我创建了 2 条路由 /upload 工作正常我可以上传图片但是 /signup 我在 req.files 中得到一个空的 obj 并且只能访问 req.body

这是我的 route.js 文件

const express = require("express");
const router = express.Router();
const app = express();
const multer = require("multer");

const storage = multer.memoryStorage();

const upload = multer({ storage });

let multiUpload = upload.fields([{ name: "photo", maxCount: 1 }]);
const { s3upload } = require("../middleware/awsService");

router.post("/signup", multiUpload, async (req, res) => {
  console.log(req.files);
  console.log(req.body);
  // const file = req.files.photo[0];
  // console.log(req.files.photo);
  const result = await s3upload(file);
  // console.log(req.files);
  // console.log(req.body);
  res.status(200).json({
    status: "success",
    message: "Upload successfull",
    result,
  });
});

router.post("/upload", multiUpload, async (req, res) => {
  const file = req.files.photo[0];
  console.log(req.files.photo);
  const result = await s3upload(file);
  console.log(req.files);
  console.log(req.body);
  res.status(200).json({
    status: "success",
    message: "Upload successfull",
    result,
  });
});

module.exports = router;

/upload 工作正常但无法通过 /signup 路由上传文件

javascript node.js express multer
© www.soinside.com 2019 - 2024. All rights reserved.