npm i express npm i multer
const express = require("express"); const multer = require("multer"); var storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, 'uploads/') }, filename: function (req, file, cb) { var fileFormat = (file.originalname).split("."); cb(null, file.fieldname + '-' + Date.now() + "." + fileFormat[fileFormat.length - 1]); } }) const upload = multer({ storage: storage }); const app = express(); app.use(express.json()); app.use(express.urlencoded({ extended: true })); app.post("/upload_files", upload.array("files"), uploadFiles); function uploadFiles(req, res) { console.log(req.body); console.log(req.files); res.json({ code: 1, data: req.files}); } app.listen(5000, () => { console.log(`Server started...`); });
来源:https://blog.logrocket.com/multer-nodejs-express-upload-file/