I'm using multer package to receive multiple images from the frontend (ReactJS).I'm sending in additional to basic fields, an array of Images and I want to save them In my Rest API server using node. I'm receiving my body fields but the problem is the images are not received. Here is my code:
const multer = require('multer');
const storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, './uploads/');
},
filename: function(req, file, cb) {
cb(null, new Date().toISOString() + file.originalname);
}
});
const fileFilter = (req, file, cb) => {
if(file.mimetype === 'image/jpeg' || file.mimetype === 'image/png'){
cb(null, true);
} else {
cb(new Error('worng file format'), true);
}
}
// initialize multer
const upload = multer(
{
storage: storage,
fileFilter: fileFilter,
}
);
router.get('/', categoryController.getCategories);
router.post('/', upload.array('images', 3), (req, res, next) => {
try {
// here I want the images to save their location
console.log(req.files);
const name = req.body.name;
const description = req.body.description;
} catch (err) {
}
});
Here is how I'm sending the images array:
The folder upload
where my images should be, is empty;
So, how do I save my multiple images ?
Append each file individually to the form:
for (const file of selectedFiles) {
formData.append('image', file);
}