7

I was wondering if it is possible to get Busboy to parse the fields and files separately. (I have removed bodyParser as you can fill the hard drive with temp files very easily.)

For instance - middleware which parsers post fields (used on all POST requests)

if (req.method === 'POST') {
    var form = new busboy({ headers: req.headers, limit: { files: 0 } });

    form.on('field', function (fieldname, val, valTruncated, keyTruncated) {
        req.params.body[fieldname] = val;
    });
    form.on('finish', function () {
        next();
    });
    return req.pipe(form);

} else { next(); }

Then on upload pages use the following, which uses Busboy to get the posted files.

app.post('/fm/file/upload/:folder', function (req, res) {
    var isFrame = helpers.toBool(req.param('frame'), false),
        uploadService = fmUploadService.create(),
        userId = res.locals.info.User.Id,
        folder = helpers.toInt(req.param('folder', 0));

uploadService.processUploads(userId, folder, req, res, function (uploadError, allowedAccess, files) {
        if (uploadError) {
            if (req.xhr) {
                res.status(500);
                res.json(uploadError);
            } else {
                res.render('filemanager/file_upload.jade', { actionUrl: '/fm/file/upload/' + folder, tempData: files, isFrame: isFrame, err: uploadError });
            }
            return;
        }
        else if (req.xhr) {
            res.status(200);
            res.json(files);
            return;
        }
        res.render('filemanager/file_upload.jade', { actionUrl: '/fm/file/upload/' + folder, tempData: files, isFrame: isFrame, err: null });
    });
});

Currently files will always be 0 as Busboy has already run within the middleware.

3
  • 3
    Did you ever find a solution for this? I have the same use case, want to parse files in my model, but catch the fields as middleware to populate the req.body before hitting the routes.
    – Scott
    May 14, 2014 at 1:17
  • 2
    I am looking for a solution for this. Any updates? Oct 14, 2014 at 6:27
  • I'm not sure if I understand the question correctly, but in Express 4, Multer handles the file uploads separately.
    – sluijs
    Sep 11, 2015 at 12:15

1 Answer 1

0

Instead of

form.on('finish', function () {
    next();
});`

try it with

form.on('end', function () {
    next();
});`   

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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