【发布时间】:2020-09-14 08:28:20
【问题描述】:
Multer中是否有任何功能可以在请求被取消时从服务器中删除文件的上传部分
【问题讨论】:
Multer中是否有任何功能可以在请求被取消时从服务器中删除文件的上传部分
【问题讨论】:
长期以来,这一直是 multer 中的一个错误。请在 github 上查看此问题:https://github.com/expressjs/multer/issues/259,对话中列出了各种解决方法。
在我们的项目中,我们目前正在使用 in this comment 提到的 fork "kyleerik/multer#kyleerik-patch-1",到目前为止它运行良好。
【讨论】:
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');
})
}
【讨论】: