如何在multer上传文件之前获取正文?

时间:2017-01-15 16:38:15

标签: javascript node.js express

我在NodeJS项目中使用 multipart / form-data ,让用户上传MP3并设置设置。

我决定使用multer中间件工作正常。 https://github.com/expressjs/multer

index.ejs

<form method="post" action="/acp" role="publish" enctype="multipart/form-data">
    <div class="form-group">
        <input type="file" name="song" id="song" accept="audio/mpeg">
    </div>

    <input type="checkbox" name="gender" checked data-toggle="toggle" data-on="Male" data-off="Female">
</form>

app.js

var uploadSong = upload.single('song');
app.post('/acp', isLoggedIn, function (req, res) {

    console.log(req.body.gender); // returns "undefined"

    if(req.body.gender == 0) { // returns "undefined"

        uploadSong(req, res, function (err) {
            if (err) {
                res.send('uploaded');
                return;
            }
            res.redirect('/');

        });

    }
});

问题 req.body.gender 始终未定义,因为我必须在 uploadSong 侦听器中使用它。

我想在性别为零时上传歌曲。

3 个答案:

答案 0 :(得分:1)

(A)multer不可能。

(B)使用busboy。它使用流来解析表单数据,因此您可以在文件上载之前获取表单元素值,并将字段作为事件提供。

(C)另一个解决方案(如果你更喜欢使用multer)是使用multer但是添加一个头来发送参数的值以在文件上传之前检查。请求到达服务器后,标题即可用。

答案 1 :(得分:1)

通过使用 multer form-data parser,您可以在 multer 开始之前解析表单并访问 req.body,只需注册此应用中间件:

import * as multer from "multer";

// parse form-data
app.use(multer().any());

答案 2 :(得分:0)

这是我的示例代码,它很好,如果您需要进一步说明,请告诉我。希望对你有帮助。

var Hotel = require('../models/hotel');
    var path = require('path');
    var multer = require('multer');
    var uplodedImages = [];

    var storageHotelGallery = multer.diskStorage({
      destination: function (req, file, cb) {
        cb(null, './uploads/hotelGallery');
      },
      filename: function (req, file, cb) {
        console.log(req.body);
        var newFileName = Date.now() + path.extname(file.originalname);
        req.newFileName = newFileName;
        cb(null, newFileName);
        uplodedImages.push(newFileName);
      }
    });
    var uploadHotelGallery = multer({ storage: storageHotelGallery}).fields([{ name: 'imgArr', maxCount: 8 }]);

    module.exports = function(router) {
        // ADD HOTEL BASIC DATA TO CREATE HOTEL OBJECT
        router.post('/createHotelStep1', function(req, res) {
            if( req.body.name == null || req.body.name == '' ) {
                res.json({ success: false, message: "Hotel name required" });
                res.end();
            }
            else if( req.body.addressline1 == null || req.body.addressline1 == '' ) {
                res.json({ success: false, message: "Address line 1 is required" });
                res.end();
            }
            else if( req.body.city == null || req.body.city == '') {
                res.json({ success: false, message: "City is required" });
                res.end();
            }
            else {
                var hotel = new Hotel();
                hotel.name = req.body.name;
                hotel.addressline1 = req.body.addressline1;
                hotel.addressline2 = req.body.addressline2;
                hotel.phone = req.body.phone;
                hotel.city = req.body.city;
                hotel.email = req.body.email;
                hotel.save(function(err) {
                    if (err) {
                        res.json({ success: false, message: "Unable to Complete Hotel Step 1" });
                    } else {
                        res.json({ success: true, message: 'Create Hotel Step 1 Complete', _id : hotel._id });
                    }
                });
            }
        });

        router.post('/createHotelGallery', function (req, res, next) {
            uplodedImages = [];
            uploadHotelGallery(req, res, function(err) {
                if(err) {
                    res.json({ success: false, message: 'Could not upload images'});
                    res.end();
                }
                else {
                    Hotel.findOne({ _id:req.body._id }).populate('users').exec(function (err, hotel) {
                    if (err) {
                        res.json({ success: false, message: 'Could not save uploaded images to database'});
                        res.end();  
                    }
                    else {
                        for(var x=0; x<uplodedImages.length; x++)
                            hotel.images.push(uplodedImages[x]);
                        hotel.save();
                        res.json({ success: true, message: 'Gallery image uploaded' });
                        res.end();
                    }
                    });
                }
            });
        });

        return router;
    }