Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

busboy not firing field, file event

In below code, only finish event called.


var Busboy = require('connect-busboy');

app.use(Busboy());

app.post('/fileupload', function(req, res) {
  var fstream;

  req.pipe(req.busboy);

  req.busboy.on('error', function(err){
    console.log(err);
  });

  req.busboy.on('field', function(fieldname, val, valTruncated, keyTruncated) {
    console.log("fieldname: " + fieldname); 
  });

  req.busboy.on('file', function (fieldname, file, filename) {
    console.log("filename: " + filename); 
    fstream = fs.createWriteStream(__dirname + '/files/' + filename);
    file.pipe(fstream);
    fstream.on('close', function () {
      res.redirect('back');
      console.log("fileupload end");
    });
  });

  req.busboy.on('finish', function() {
    console.log('Done parsing form!');
  });
});
like image 796
Prayag C. Patel Avatar asked Sep 30 '22 14:09

Prayag C. Patel


1 Answers

The reason why you're not seeing any data is because you're already using the multer module which also parses multipart/form-data requests, saving files to disk. If you're not using multer and want to use busboy manually as you show in your code, you will need to remove the app.use(multer()); line.

like image 175
mscdex Avatar answered Oct 03 '22 02:10

mscdex