Skip to content

Conversation

KaustubhBankar
Copy link

//In public / frontend changes -> added button for attachment;

const handleFileChange = (event) => {
setFile(event.target.files[0]); // Save the selected file to state
};

// In backend ---->

const multer = require("multer");
const path = require("path"); // Add this line to import the path module
const fs = require("fs");

const uploadDir = path.join(__dirname, "uploads");

// Check if the uploads directory exists, if not, create it
if (!fs.existsSync(uploadDir)) {
fs.mkdirSync(uploadDir, { recursive: true });
console.log(Created uploads directory at ${uploadDir});
}

// Multer configuration
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "uploads/"); // Where files will be stored
},
filename: (req, file, cb) => {
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
cb(null, file.fieldname + '-' + uniqueSuffix + path.extname(file.originalname)); // Rename the file with a unique suffix
}
});

const upload = multer({ storage: storage });

module.exports = upload;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant