Why is the Formidable `form.parse` callback not running in Express?
Asked Answered
P

6

17

I am using the formidable package to handle file uploads on my server. This is my express.js app code:

var formidable = require("formidable"),
  http = require("http"),
  util = require("util");
app.post("/admin/uploads", function (req, res) {
  console.log(req.files, req.fields); //It prints
  var form = new formidable.IncomingForm();
  form.parse(req, function (err, fields, files) {
    console.log("Inside form parse."); //its not printing
    console.log(err, fields, files); //its not printing
  });
  form.on("file", function (name, file) {
    console.log("file=" + file);
  }); //its not printing
  form.on("error", function (err) {
    console.log(err);
  }); //its not printing
  form.on("aborted", function () {
    console.log("Aborted");
  }); //its not printing
  console.log(form); //it prints
});

In the above code, the form.parse and form.on callbacks are never run. How can I solve this issue?

Poucher answered 15/11, 2012 at 13:52 Comment(0)
C
33

I had the same problem when using Next.js API routes with Formidable. As another answer points out, you have to remove the body parser. In Next.js, you can export a config object and disable body parsing.

// /pages/api/form.js
import { IncomingForm } from "formidable";

export default function handler(req, res) {
  // formidable logic
}

// VV important VV
export const config = {
  api: {
    bodyParser: false,
  },
};

Note that if your project uses the newer app router, you won’t need Formidable; you can handle form submissions using FormData in server actions / mutations.

Carrillo answered 30/8, 2021 at 18:28 Comment(1)
This answer was actually helpful for me. One thing, as a Next.js newbie I tried to export the config from my current file. You actually need to export it from the api handler to workMetrist
E
13

It might be that you need to remove body parser

delete app.use(express.bodyParser());
Ednaedny answered 22/1, 2013 at 13:22 Comment(1)
That was exactly the reason in my case. Thanks a lot!Single
J
1

Please add the error handlers and send the error message otherwise it is hard to get an answer.

form.on('error', function(err) { console.log(err); });
form.on('aborted', function() { console.log('Aborted'); });

See the formidable documentation : doc

Joycelynjoye answered 15/11, 2012 at 14:13 Comment(1)
Nothing is printing inside form methods.Poucher
M
1

Call form.parse(...) after all on(...) events.

app.post('/admin/uploads', function(req, res) {
    var form = new formidable.IncomingForm(); 
    form.on('file', function(name, file) { });
    form.on('error', function(err) { });
    form.on('aborted', function() { });
    form.parse(req, function(err, fields, files) { });
});
Mcneil answered 10/1, 2015 at 11:0 Comment(0)
V
1

I forgot to add enctype="multipart/form-data" to my html form, maybe this will help somebody :)

Voccola answered 23/3, 2020 at 20:57 Comment(1)
That is a pretty general pitfall: please argue why this might well have been the problem here, given the absence of client side code.Zambrano
C
0

Even if you remove body-parser and use express.json() etc... you will have same error.

The problem is that express.raw() is causing trouble remove it and it works!

Cleanlimbed answered 8/7, 2022 at 8:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.