2

When i send a file to /upload, from the terminal I can see that the code is blocked at form.parse(req... On the Internet I read that this is because I use bodyParser(), and people suggested to do this:

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

however when I do that my code crashes since parse is null.

Anyone has any idea on how to get file upload working only in /upload and possibly with formidable?

Thanks.

This is my app.js:

'use strict';

var express = require("express");
var async = require("async");
var http = require('http');
var url = require("url");
var qs = require("querystring");
var fs = require("fs");
var formidable = require("formidable");
var mime = require("mime");

var app = module.exports = express();

app.configure(function () {
  app.set("views", __dirname + "/views");
  app.set("view engine", "ejs");
  app.engine("html", ejs.renderFile);
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser("abc"));
  app.use(express.static(__dirname + "/static"));
  app.use(app.router);
  app.use(express.limit('5mb'));
});

app.post('/upload', storage.upload);

This is my code storage.js:

'use strict';

var async = require('async');
var formidable = require('formidable');
var fs = require('fs');
var util = require('util');
var api = require('./common');

exports.upload = function (req, res) {

  console.log("I am here");

  var form = new formidable.IncomingForm(),
    files = [],
    fields = [];

  form.uploadDir = "./uploads";

  form
    .on('field', function (field, value) {
      console.log(field, value);
      fields.push([field, value]);
    })
    .on('error', function (err) {
      res.writeHead(200, {'content-type': 'text/plain'});
      res.end('error:\n\n'+util.inspect(err));
    })
    .on('file', function (field, file) {
      console.log(field, file);
      files.push([field, file]);
    })
    .on('aborted', function (err) {
      console.log("user aborted upload");
    })
    .on('end', function () {
      console.log('-> upload done');
      res.writeHead(200, {'content-type': 'text/plain'});
      res.write('received fields:\n\n '+util.inspect(fields));
      res.write('\n\n');
      res.end('received files:\n\n '+util.inspect(files));
    });
  form.parse(req);
};

2 Answers 2

4

If you want to use formidable then, remove app.use(express.bodyParser()); & add app.use(express.json());app.use(express.urlencoded());

Connect 3.0

This solution worked for me, it might be helpful for other dev.

2

Have you seen the example on github: https://github.com/visionmedia/express/blob/master/examples/multipart/index.js

I've modified it to run on Express 3.x and it seems to work smoothly:

var express = require('express');
var fs = require('fs');
var app = express();
var format = require('util').format;


// bodyParser in connect 2.x uses node-formidable to parse 
// the multipart form data.
app.use(express.bodyParser())

app.get('/', function(req, res){
  res.send('<form method="post" enctype="multipart/form-data">'
    + '<p>Title: <input type="text" name="title" /></p>'
    + '<p>Image: <input type="file" name="image" /></p>'
    + '<p><input type="submit" value="Upload" /></p>'
    + '</form>');
});

app.post('/', function(req, res, next){
  // the uploaded file can be found as `req.files.image` and the
  // title field as `req.body.title`
  res.send(format('\nuploaded %s (%d Kb) to %s as %s'
    , req.files.image.name
    , req.files.image.size / 1024 | 0 
    , req.files.image.path
    , req.body.title));
});

if (!module.parent) {
  app.listen(3000);
  console.log('Express started on port 3000');
}
3
  • In this way for all app.post("*") I will have the file will be uploaded on my server, thing that I want to avoid
    – piggyback
    Commented Feb 6, 2013 at 15:29
  • yeah, this is just a working sample of the upload file feature. I didn't mean for you to use it verbatim but instead to compare it with your code and see how come upload works in one but not the other. Commented Feb 6, 2013 at 16:40
  • For my issue I just needed to add: enctype="multipart/form-data" to my from tag. Thanks heaps! My problem was that my file was being read as another field.
    – Worm
    Commented Nov 6, 2019 at 16:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.