Node.js Busboy分析字段和文件 [英] Node.js Busboy parse fields and files seperatly

查看:145
本文介绍了Node.js Busboy分析字段和文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.)

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

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(); }

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

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 });
    });
});

目前,文件总是为0,因为Busboy已经在中间件中运行。

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

推荐答案

而不是

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

尝试使用

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

这篇关于Node.js Busboy分析字段和文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆