expressjs / expressjs/multer

How to upload a Image and a Video file to different destination recieved via same request .

Open
#1,184 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
12.1k
Forks
1.1k
Avg merge
8d 2h
Merged PRs (30d)
21

Description

Destination For Image Storage
`const imageStorage = multer.diskStorage({
destination: "./assets/Images",
filename: (req, file, cb) => {
cb(
null,
"Image-" +
file.originalname.split(".")[0] +
"-" +
Date.now() +
"." +
file.originalname.split(".")[1]
);
},
});

const upload_image = multer({
storage: imageStorage,
limits: {
fileSize: 10000000,
},
fileFilter(req, file, cb) {
if (!file.originalname.match(/\.(png|jpg|JPG|PNG|webp|WEBP)$/)) {
return cb(new Error("Only Png / Jpg / Webp format is accepted"));
}
cb(undefined, true);
},
});
`

Destination For Video Storage
`const videoStorage = multer.diskStorage({
destination: "./assets/Videos",
filename: (req, file, cb) => {
cb(
null,
"Video-" +
file.originalname.split(".")[0] +
"-" +
Date.now() +
"." +
file.originalname.split(".")[1]
);
},
});
const upload_video = multer({
storage: videoStorage,
limits: {
fileSize: 100000000,
},
fileFilter(req, file, cb) {
if (!file.originalname.match(/\.(mp4|mov)$/)) {
return cb(new Error("Only mp4 / mov format is accepted"));
}
cb(undefined, true);
},
});`

I am doing this but this is giving me error
`router.post(
"/add-image-video-post",
upload_image.array("Image", 1 ), upload_video.fields("Video", 1 ) ,
async (req, res) => {
console.log(req.files);
return res.status(200).json({ success: true });
}
);
`
![image](https://user-images.githubusercontent.com/75539409/215608328-89c8edd8-6f4a-4f02-8dcc-9d7abdd86d6d.png)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.