Last active
April 3, 2023 07:40
-
-
Save Wildanzr/8065d7a2e28a9b6166cd124430df5751 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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