Replies: 1 comment
-
I have found the answer do not use upload multiple in dropzone for sending the multiple files for multer use. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
// index.html
<title>Multiple File Upload</title> <style> .dropzone { border: 2px dashed #0087f7; border-radius: 5px; background: white; min-height: 150px; padding: 20px; box-sizing: border-box; } </style>Upload Multiple Files
// server.js
const express = require("express");
const multer = require("multer");
const path = require("path");
const cors = require("cors");
const fs = require("fs");
const app = express();
const port = 3000;
// Enable CORS
app.use(cors());
// Create uploads directory if it doesn't exist
const uploadDir = path.join(__dirname, "uploads");
if (!fs.existsSync(uploadDir)) {
fs.mkdirSync(uploadDir, { recursive: true });
}
// Configure multer storage
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, uploadDir);
},
filename: function (req, file, cb) {
// Create unique filename
const uniqueSuffix = Date.now() + "-" + Math.round(Math.random() * 1e9);
cb(null, uniqueSuffix + "-" + file.originalname);
},
});
// Configure multer upload
const upload = multer({
storage: storage,
limits: {
fileSize: 100 * 1024 * 1024, // 100MB limit
files: 10, // Maximum 10 files
},
fileFilter: function (req, file, cb) {
// Check file types
const filetypes = /jpeg|jpg|png|pdf/;
const mimetype = filetypes.test(file.mimetype);
const extname = filetypes.test(
path.extname(file.originalname).toLowerCase()
);
},
});
// Serve static files
app.use(express.static(__dirname));
// Handle multiple file upload
app.post("/upload", (req, res) => {
upload.array("files", 10)(req, res, function (err) {
if (err instanceof multer.MulterError) {
// A Multer error occurred
console.error("Multer error:", err);
return res.status(400).json({
error: true,
message: err.message,
});
} else if (err) {
// An unknown error occurred
console.error("Unknown error:", err);
return res.status(500).json({
error: true,
message: err.message,
});
}
});
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error("Error:", err);
res.status(500).json({
error: true,
message: err.message || "Internal server error",
});
});
// Start server
app.listen(port, () => {
console.log(
Server running at http://localhost:${port}
);});
it gives me the error of unexpected field .
What is the error in it
image is here ---
Beta Was this translation helpful? Give feedback.
All reactions