Search code examples
angularjsnode.jsxmlhttprequestbusboy

Busboy doesn't fire 'field' event


I get the file event of the formData but busboy doesn't fire the other field event. Can someone take a look? I'm using the newest version of node and busboy. The formData is posted via xmlhttprequest. I want to add some other fields than just the file.

Busboy:

var Busboy = require('busboy'),
    path = require('path'),
    Connection = require('ssh2'),
    fs = require('fs');

module.exports = {
    uploadFile: function(req,res) {

        var conn = new Connection();
        conn.on('ready', function() {
            console.log('Connection :: ready');
            conn.sftp(function(err, sftp) {
                var busboy = new Busboy({ headers: req.headers });
                req.pipe(busboy);

                busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
                    //works fine
                });
                busboy.on('ident', function(fieldname, val) {
                    // doesn't get called
                });
                busboy.on('finish', function() {
                    res.status(200).end();
                    console.log('Done parsing form!');
                });

            });
        }).connect({
            //options
        });

        conn.on('error', function (err) {
            console.log( "- connection error: %s", err );
            process.exit( 1 );
        });


    }
}

Formdata:

        var file = $scope.files[0];
        var fd = new FormData();
        fd.append('file', file);
        fd.append('ident', $routeParams.id);
        if (file.type!="application/pdf"){
            mvNotifier.error("Nur PDF Dateien sind akzeptiert.");
            return true;
        }       
        var xmlhttp;
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.open('POST', '/api/upload/file', true);
        xmlhttp.onload = function(response) {
             //gets called
        }
        xmlhttp.send(fd);

Solution

  • not

    busboy.on('ident', function(fieldname, val) {}));
    

    you must try:

    busboy.on('field', function(fieldname, val) {
                 /*get all non-file field here*/ });