Is there Any Function in Multer To Remove uploaded part of the File from the server if The Request gets Cancelled
Is there Any Function in Multer To Remove uploaded part of the File from the server if The Request gets Cancelled
This has been a bug in multer for a very long time. See this issue on github: https://github.com/expressjs/multer/issues/259, where various workarounds are listed in the conversation.
In our project we're currently using the fork "kyleerik/multer#kyleerik-patch-1" which is mentioned in this comment and it has been working fine so far.
filename: (req, file, cb) => {
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1e9);
const fileName =
file.fieldname + '-' + uniqueSuffix + path.extname(file.originalname);
cb(null, fileName);
req.on('aborted', () => {
const fullFilePath = path.join('uploads', 'videos', fileName);
file.stream.on('end', () => {
fs.unlink(fullFilePath, (err) => {
console.log(fullFilePath);
if (err) {
throw err;
}
});
});
file.stream.emit('end');
})
}