我在使用 multi.上传文件到服务器时遇到问题。 我阅读了文档并根据文档编写了代码,但文件仍然没有上传到文件夹 images
我的代码:
const express = require("express");
const app = express();
const path = require("path");
const multer = require("multer");
const storage = multer.diskStorage({
desination: "images",
filename: (req, file, cb) => {
console.log(file);
cb(null, Date.now() + path.extname(file.originalname));
},
});
const upload = multer({ storage: storage });
app.set("view engine", "ejs");
app.get("/upload", (req, res) => {
res.render("upload");
});
app.post("/upload", upload.single("image"), (req, res) => {
res.send("Image Uploaded");
});
app.listen(3001);
console.log("3001 is the port");
你可以在 cb 之前添加 return,这对我有用。
const storage = multer.diskStorage({
desination: "images",
filename: (req, file, cb) => {
console.log(file);
return cb(null, Date.now() + path.extname(file.originalname));
},
});`