Skip to content

Instantly share code, notes, and snippets.

@Wildanzr
Last active April 3, 2023 07:40
Show Gist options
  • Save Wildanzr/8065d7a2e28a9b6166cd124430df5751 to your computer and use it in GitHub Desktop.
Save Wildanzr/8065d7a2e28a9b6166cd124430df5751 to your computer and use it in GitHub Desktop.
// Specifies the file upload location
const diskStorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, path.join(__dirname, "uploads"));
},
filename: (req, file, cb) => {
cb(
null,
`${file.fieldname}-${Date.now()}${path.extname(file.originalname)}`
);
},
});
// Create function upload with multer
const upload = multer({
storage: diskStorage,
limits: {
fileSize: 1024 * 1024 * 25, // Max file size 25MB
},
});
// Create function to delete file
const deleteFile = (file) => {
fs.unlink(file.path, (err) => {
if (err) {
console.error(err);
throw new Error("Failed to delete file");
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment