>我在 MERN Stack 应用程序中使用 Multer 在 POST 时上传多个文件以创建新的图像库。
router.post('/gallery', upload.array('photos', 5), galleryController.createGallery);
但是,在我确认此用户的库尚不存在之前,我不希望上传文件。
有谁知道如何延迟文件上传,直到我确认应该创建此图库?或者,如果这是不可能的,有没有办法"回滚"上传并从此上传中删除图像?
感谢您的任何指导。
当您使用任何预定义的默认值(如.single()
、.array()
、.fields()
等(时,您无法对上传进行任何形式的控制。
您需要提供自定义storage
引擎才能完成您所要求的内容。请参阅此处的文档:https://www.npmjs.com/package/multer#storage
const storage = multer.diskStorage({
destination: (req, file, cb) => {
// Do logic to delay the file upload if needed by throwing error
},
filename: (req, file, cb) => {
// ...
}
})
const upload = multer({ storage: storage })