将表单类型更改为multipart / form-data会导致req.body为空

时间:2014-01-08 17:16:18

标签: json forms node.js express

当我在未指定enctype的情况下设置表单时,Firefox会自动将其设置为application/x-www-form-urlencoded,而req.body包含输入到表单中的所有参数的精美JSON表示。但是当我将enctype更改为multipart/form-data时,req.body突然变空。

这是我的表格:

<form action="/create" method="post" enctype="multipart/form-data">
  <fieldset>
    <div>
      <label>Category:</label>
    </div>
    <div>
      <select name="category">
      <option value="standard">Standard</option>
      <option value="custom">Custom</option>
    </div>
    <div>
      <input type="text" name="description">
    </div>
    <div>
      <label>User ID:</label>
    </div>
    <div>
      <input type="text" name="userid">
    </div>
    <div>
      <input type="submit" value="Go">
    </div>
  </fieldset>
</form>

console.log(JSON.stringify(req.body, null, 2));enctype并且未指定multipart/form-data时,执行enctype打印出一个空对象,它会输出如下内容:

{
  category: "standard",
  userid: "foo"
}

出现这种情况的原因是什么?

4 个答案:

答案 0 :(得分:1)

听起来您正在使用express.urlencoded()而不是express.multipart()

答案 1 :(得分:1)

我认为@robertklep是正确的,但我不同意他的回答。 express.multipart()deprecated and should not be used

如果您需要多部分表单处理,我强烈推荐Busboy。如果您需要所有详细信息,请参阅this answer

答案 2 :(得分:1)

尝试使用busboy-body-parser检索请求正文参数和文件。

start.js
var bodyParser = require('body-parser');
var busboyBodyParser = require('busboy-body-parser');


// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({
  extended: true
}));

// parse application/json
app.use(bodyParser.json());


//parse multipart/form-data    
app.use(busboyBodyParser());



controllers/someController.js
someAction: function(req,res){
     if(req.method == "POST"){
           res.end(JSON.stringify(req.body)+JSON.stringify(req.files));
     }
}
//{"text":"testx"}{"anexo":{"data":{"type":"Buffer","data":.... }}} 
//req.body = {"text":"testx"}
//req.files = {"anexo":{"data":{"type":"Buffer","data":.... }}} 


views/someController/someAction.html
<form method="post" id="multipart" enctype="multipart/form-data">
    <input type="text" id="text1" name="text" value="testx" />
    <input type="file" id="anexo" name="anexo"  />
    <input type="submit" value="Enviar" />
</form>

要创建上传的文件,您需要在流中工作,例如:

/* file props
{  
   "data":{"type":"Buffer","data":.... },
   "fieldname":"anexo",
   "originalname":"images (1).jpg",
   "encoding":"7bit",
   "mimetype":"image/jpeg",
   "destination":"c:\\live\\sources\\uploads\\",
   "filename":"eventclock_images (1)_1443706175833.jpg",
   "path":"c:\\live\\sources\\uploads\\eventclock_images(1)_1443706175833.jpg",
   "size":9986
}
*/

var fileStream = fs.createWriteStream(file.path);
fileStream.write(file.data);
fileStream.end();

fileStream.on('error', function (err) {
    //console.log("error",err);
});
fileStream.on('finish', function (res) {
    //console.log("finish",res);
});     

答案 3 :(得分:-1)

npm install multer --save

in main.js

var multer  =   require('multer');
var upload = multer()

router.route("/quotes").post(upload.array(),function(req, res, next){
        name = req.body.name;
        email = req.body.email;
}