简体   繁体   中英

Node.js Busboy parse fields and files seperatly

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.

Instead of

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

try it with

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM