最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can I use body-parser and Formidable at the same time? - Stack Overflow

programmeradmin3浏览0评论

I am tryint to resolve a problem a couple of days, but can't understand some things. I have a web site created with NodeJS and ExpressJS, and for handling forms I use body-parser.

    var adName = req.body.adName;
    var adMessage = req.body.adMessage;
    var phone = req.body.phone;
    var rawPrice = req.body.price;
    var rawCurrency = req.body.currency;

So, using this method I handle form values. But now, I need to use node-formidable to parse images from users. The question is, can I use somehow formidable only for images and body-parser for forms? Or, can anyone help me with formidable, to understand how to handle forms and attach values to my variables?

I am tryint to resolve a problem a couple of days, but can't understand some things. I have a web site created with NodeJS and ExpressJS, and for handling forms I use body-parser.

    var adName = req.body.adName;
    var adMessage = req.body.adMessage;
    var phone = req.body.phone;
    var rawPrice = req.body.price;
    var rawCurrency = req.body.currency;

So, using this method I handle form values. But now, I need to use node-formidable to parse images from users. The question is, can I use somehow formidable only for images and body-parser for forms? Or, can anyone help me with formidable, to understand how to handle forms and attach values to my variables?

Share Improve this question edited Nov 20, 2014 at 8:45 tourniquet asked Nov 18, 2014 at 14:10 tourniquettourniquet 1,4352 gold badges16 silver badges19 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

You may want to take some time out to study/practice with the formidable module. See this url: https://github./felixge/node-formidable

Yes, formidable can be used to process both form fields and file upload including multiple file uploads. body-parser middleware does not handle multiparts - https://github./expressjs/body-parser. In this case, I will advise you use formidable and drop body-parser.

See if below express app help you out.

var formidable = require('formidable'),
    util = require('util'),
    express = require('express'),
    app = express();

app.set('port', process.env.PORT || 3600);
app.get('/', function (req, res) {
    res.send(     
    '<form action="/upload" enctype="multipart/form-data" method="post">'+
    '<input type="text" name="adName" placeholder="adName"><br>'+
    '<input type="text" name="adMessage" placeholder="adMessage"><br>'+
    '<input type="text" name="phone" placeholder="phone"><br>'+
    '<input type="text" name="rawPrice" placeholder="rawprice"><br>'+
    '<input type="text" name="rawCurrency" placeholder="rawcurrency"><br>'+
    '<input type="file" name="upload" multiple="multiple"><br>'+
    '<input type="submit" value="Upload">'+
    '</form>'
  );
});

app.post('/upload', function(req, res){
    var form = new formidable.IningForm();
    form.uploadDir = __dirname + "/data";
    form.parse(req, function(err, fields, files) {
        //fields is an object containing all your fields, do waht ever you want with them from here
        //file is an object containing properties of your uploaded file
      res.send(util.inspect({fields: fields, files: files}));
      console.log('file uploaded : ' + files.upload.path + '/' + files.upload.name);
      console.log('Fields : ' + fields.adName);//you can access all your fields
    });
});

//starting server
app.listen(app.get('port'), function () {
    console.log('Express is listening: http://localhost:%s;', app.get('port'));
});

You can use both body-parser and formidable at the same time if you wish. You can use formidable just for some specific routes and continue using body-parser on the rest. Below I show the code needed to use formidable for just one route:

const formidableMiddleware = require('express-formidable');

app.post('/api/v1/uploadfile', formidableMiddleware(), async (req, res) => {
  const file = req.files.file;
  console.log('file info: ' + file);

  const fields = req.fields;
  console.log('fields = ' + JSON.stringify(fields));
});

Take a look at this link: https://github./utatti/express-formidable/issues/1

发布评论

评论列表(0)

  1. 暂无评论