Node.js Busboy分别解析字段和文件

Miz*_*rym 7 javascript upload node.js express

我想知道是否有可能让Busboy分别解析字段和文件.(我已经删除了,bodyParser因为你可以很容易地用临时文件填充硬盘驱动器.)

例如 - 解析器发布字段的中间件(用于所有POST请求)

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(); }
Run Code Online (Sandbox Code Playgroud)

然后在上传页面上使用以下内容,它使用Busboy来获取发布的文件.

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 });
    });
});
Run Code Online (Sandbox Code Playgroud)

目前文件将始终为0,因为Busboy已经在中间件中运行.

Ket*_*vya 0

代替

form.on('finish', function () {
    next();
});`
Run Code Online (Sandbox Code Playgroud)

尝试一下

form.on('end', function () {
    next();
});`   
Run Code Online (Sandbox Code Playgroud)