Getting TypeError: Cannot read property ‘content-length’ of undefined When Using Node.js Formidable

“req” parameter in form.parse() method is omitted.

(method) IncomingForm.parse(req: IncomingMessage, callback?: (err: any, fields: Fields, files: Files) => any): void

Try changing the code as below.

app.post('/fileupload', function (req, res) {
    const form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        console.log(fields.gradeNumber);
        const oldpath = files.filetoupload.path;
        const newpath = 'C:/Users/Shubh Computer/Desktop/VSCode/Grades/1/' + files.filetoupload.name;
        fs.rename(oldpath, newpath, function (err) {
            if (err) throw err; 
            res.write('File uploaded and moved!');
            res.end();
        });
    });
});

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top