nodejs formidable not triggering any events nodejs formidable not triggering any events express express

nodejs formidable not triggering any events


if you use app.use(express.bodyParser())is equivalent to:

app.use(express.json());app.use(express.urlencoded());app.use(express.multipart());

you can remove app.use(express.multipart()); then you can use formidable object.


express is using formidable internally, so your formidable doesn't get any events, if you would like to use formidable on your own you have to remove multipart/form-data from the bodyParser

i am still on express 2.x but that should also do the trick on 3.x, in your app.configure function try

app.configure(function(){  delete express.bodyParser.parse['multipart/form-data']})

now you can use formidable on your own.


Is there a specific reason why you are using formidable and not the built in bodyParser? It uses the multipart middleware and is based on formidable. Therefore most of the options from formidable can also be applied to the bodyParser. For example:

app.use(connect.multipart({ uploadDir: path }));

To answer your question, try the following code:

app.js

app.configure(function(){  app.set('port', process.env.PORT || 3000);  app.set('views', __dirname + '/views');  app.set('view engine', 'jade');  app.use(express.favicon());  app.use(express.logger('dev'));  app.use(express.bodyParser());  app.use(express.methodOverride());  app.use(app.router);  app.use(express.static(path.join(__dirname, 'public')));});app.post("/", function(req, res) {  console.log(req.files.img)  res.end()})

index.jade

form  input(type="file", name="img", id="img")  input(type="button", value="button", id="upload")script  var formdata = new FormData()  var xhr = new XMLHttpRequest()  $("#upload").on("click", function(e) {                xhr.upload.onprogress = function(evt) {      console.log("While sending and loading data.")      if (evt.lengthComputable) {        console.log (evt.loaded / evt.total * 100);      }    }    xhr.onloadend = function(evt) {      console.log("When the request has completed (either in success or failure).")    }    xhr.onload = function(evt) {      console.log("When the request has successfully completed.")    }    var image = document.getElementById('img').files[0]    formdata.append("img", image)    xhr.open("POST", "/", true)    xhr.send(formdata)  })

Have a look at the W3C Specifications for more events.