expressjs / expressjs/multer

Error while uploading Image and Video File together !

Open
#1,180 1 comment 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

Form Data
![image](https://user-images.githubusercontent.com/75539409/214525090-bfd118c3-844e-4a12-b52b-2e59308de3a6.png)

**This is the code to upload image and upload video**
`
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);
},
});

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);
},
});
`

**Code of the request**
`router.post(
"/add-image-video-post",
upload_image.fields([{ name: "Image", maxCount: 1 }]),
upload_video.fields([{ name: "Video", maxCount: 1 }]),
async (req, res) => {
console.log(req.files);
return res.status(200).json({ success: true });
}
);
`
**Error Image**
![image](https://user-images.githubusercontent.com/75539409/214524930-c591f1a4-79f8-4aae-a72e-274f754b2c38.png)

**Error Text**
**Error: Unexpected end of form
at Multipart._final (C:\Users\Hp\Desktop\twitter\backend\node_modules\busboy\lib\types\multipart
.js:588:17)
at callFinal (node:internal/streams/writable:694:27)
at prefinish (node:internal/streams/writable:723:7)
at finishMaybe (node:internal/streams/writable:733:5)
at Multipart.Writable.end (node:internal/streams/writable:631:5)
at onend (node:internal/streams/readable:693:10)
at processTicksAndRejections (node:internal/process/task_queues:78:11)
**

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.