简体   繁体   中英

Node.js Formidable :: How to parse form 'multipart/form-data' with (php convention) array fields

在此处输入图片说明

Getting req.body blank and when I parse with formidable, it makes key and value like:

key : filter[phone] 
value : 978200000

key : filter[name] 
value : Shubham

Though there were some hacks & fixes for certain scenarios, the case of your question was a continuing bug in Formidable which is fixed now: release 2.0.0-canary.20200504.1 .

All you need to do is to turn on the option multiples when you're creating an instance of formidable.

For example, assuming you're receiving the request ( req ) via express , you can do something like this:

const formidable = require('formidable');

const form = formidable({ multiples: true });

var inputFields = {};

form.parse(req, (err, fields, files) => {
  if (err) {
    next(err);
    return;
  }
  inputFields = fields;
});

As in Javascript ( unlike PHP) arrays are more literal representation stacks and objects are the goto way of storing everything, the inputFields which have string key value will be returned as object. So, for your example, inputFields will contain the following property:

filter {
  phone: '978200000',
  name: 'Shubham',
}

Also to be noted, formidable in the above mentioned way returns string .

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