0
var multer  = require('multer');
var mwMulter1 = multer({ dest: './uploads1/' });
app.post('/files1', mwMulter1, function(req, res) {
    // check req.files for your files
});
var mwMulter2 = multer({ dest: './uploads2/' });
app.post('/files2', mwMulter2, function(req, res) {
    // check req.files for your files
});
app.post('/test', function(req, res){
    var name = req.body.name;
    console.log("Name is --- "+name);
    res.json("name sent to server");
});

每当使用 multer 的多个实例时,发布数据都不会显示未定义的格式。

4

1 回答 1

0

您需要使用 multer 中间件。尝试这个:

应用程序.js:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');

app.use(bodyParser());

app.get('/', function (req, res) {
  require('fs').readFile('index.html', function(error,d) {
    res.send(d.toString());
  });
});

var multer  = require('multer');
var mwMulter1 = multer({ dest: './uploads1/' }).single("name");
app.post('/files1', mwMulter1,function( req,res){
    console.log(req.file);
    res.end();   
});

var mwMulter2 = multer({ dest: './uploads2/' }).array("name[]",2);
app.post('/files2', mwMulter2, function(req, res) {
    console.log(req.files);
    res.end();
});

app.post('/test', function(req, res){
    var name = req.body.name;
    console.log("Name is --- "+name);
    res.json("name sent to server");
});

var server = app.listen(3000, function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);
});

索引.html:

Action: test
<form name="fileupload" action="test" method="POST"  enctype="multipart/form-data">
<input name="name" type="file"/>
<button>submit</button>
</form>
<hr/>
Action: files1
<form name="fileupload2" action="files1" method="POST"  enctype="multipart/form-data">
<input name="name" type="file"/>
<button>submit</button>
</form>
<hr/>
Actions: files2
<form name="fileupload3" action="files2" method="POST"  enctype="multipart/form-data">
<input name="name[]" type="file"/><br/>
<input name="name[]" type="file"/>
<button>submit</button>
</form>
于 2015-07-20T12:38:16.110 回答