延迟 Multer 文件上传

Delay Multer file upload

我在 MERN Stack 应用程序中使用 Multer 在发布创建新图片库时上传多个文件。

router.post('/gallery', upload.array('photos', 5), galleryController.createGallery);

但是,在确认该用户不存在图库之前,我不想上传文件。

有谁知道如何延迟文件上传,直到我确认应该创建这个图库?或者,如果这不可能,是否有办法 "rollback" 上传并从此上传中删除图像?

感谢您的指导。

当您使用任何预定义的默认设置(例如 .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 })