2

Using the code below, when I try uploading a file, the browser just keeps loading forever. Any idea why?

app.js

var formidable = require('formidable'),
    http = require('http'),
    util = require('util');

app.get('/song/add', function (req, res) {
    res.writeHead(200, {'content-type': 'text/html'});
    res.end(
        '<form action="/song/upload" enctype="multipart/form-data" method="post">'+
        '<input type="text" name="title"><br>'+
        '<input type="file" name="upload" multiple="multiple"><br>'+
        '<input type="submit" value="Upload">'+
        '</form>'
    );
});

app.post('/song/upload', function (req, res) {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        res.writeHead(200, {'content-type': 'text/plain'});
        res.write('received upload:\n\n');
        res.end(util.inspect({fields: fields, files: files}));
    });
    return;
});

4 Answers 4

2

Just remove that line( app.use(express.bodyParser()) ) in your code, that should work I believe.

But why do you want to use formidable? I believe express body parser is based on formidable, and it is more cleaner to use. Since it even parses other types of body, beside the forms type like JSON, but formidable will throw an error if the body type is not supported. If you have app.use(express.bodyParser()) in your code, then you can do this to get what you want:

app.post('/song/upload', function(req, res){
    res.writeHead(200, {'content-type': 'text/plain'});
    res.write('received upload:\n\n');
    res.end(util.inspect(req.body));
    return;
 });
Sign up to request clarification or add additional context in comments.

Comments

1

You probably have the express body parser option turned on. Check your config for something like:

app.use(express.bodyParser())

I think the problem is that Formidable is expecting stream events for this file & they've already been consumed by Express by the time your app.post callback is called. Since the form.parse callback only fires on end no response is ever rendered.

The solution would be to use Express or Formidable, you can disable the config option if you want to use Formidable.

3 Comments

I had turn on app.use(express.bodyParser()) in my code.. Could u tell me detail of disable the config option if you want to use Formidable
If you want to use Formidable (behind the scene) along with Express (without app.use(express.bodyParser()), you could use connect-form. But this module, according the author, is now deprecated. See Form Handling - Processing in Express.js for a nice explanation on this subject.
See Handle File Uploads in Express (Node.js) too. It is the sequel of the above mentioned article.
0

The problem what I see here is new body parser (since Connect 1.8.0). If you want to use new Connect with formidable you need to delete multipart parser from body parser:

delete express.bodyParser.parse['multipart/form-data'];

1 Comment

When I try to add this line of code it crashes my app - I get: /Code/myApp/server/main.js:6 delete express.bodyParser.parse['multipart/form-data']; ^ TypeError: Cannot convert null to object at Object.<anonymous> (/Code/myApp/server/main.js:6:26) at Module._compile (module.js:446:26) at Object..js (module.js:464:10) at Module.load (module.js:353:31) at Function._load (module.js:311:12) at Module.require (module.js:359:17) at require (module.js:375:17) etc
0

Don't use bodyParser with Express 4.x: http://andrewkelley.me/post/do-not-use-bodyparser-with-express-js.html

use

app.use(express.json());

app.use(express.urlencoded());

instead. The problem with your code is that you will return before the callback will be called so the result will not be send to the user.

Change your code to:

return res.end(util.inspect({fields: fields, files: files}));

and remove the return at the end.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.