Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Error uploading file with node.js

Error uploading file with node.js

Problem

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(
        '
'+ '
'+ '
'+ ''+ '
' ); }); 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; });
Problem courtesy of: Huy Tran

Solution

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;
 });
Solution courtesy of: Farid Nouri Neshat

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Error uploading file with node.js

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×