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实例时,发布的数据都不会显示为未定义的格式。
答案 0 :(得分:0)
您需要使用multer中间件。试试这个:
<强> app.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);
});
<强>的index.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>