I need to be able to upload a single image AND an array of images (both are optional fields on the frontend so I want to keep them separate). My code works if I use one upload or the other. How do I properly combine upload.single('frontImage')
and upload.array('files[]')
?
router.post(
'/create',
upload.single('frontImage').array('files[]'), /// <-- HOW DO I WRITE THIS LINE?
[check('title').not().isEmpty()],
flashCardsControllers.createFlashCard
);
You can use .fields()
method which handles multiple file fields with different field names. A sample would look something like:
router.post(
'/create',
upload.fields([{ name: 'frontImage', maxCount: 1 }, { name: 'files[]' }]),
[check('title').not().isEmpty()],
flashCardsControllers.createFlashCard
);
This will allow you to handle both frontImage
and files[]
fields when uploading files. The maxCount
property is used to limit the number of files for a specific field, in this case, we set it to 1
for the frontImage
field.