我试图使用multer File Uploads with Express 4.0但是经常失败 - 我也尝试了许多其他的东西,但它没有用。请让我知道我在代码中做了什么错误
以下是我的HTML表单:
<body>
<h1 align="center">Batches</h1>
<form action="/batch" enctype="multipart/form-data">
<p class="formfield">
<label for="batch">Select Batch </label>
<select name="batch" id="batch">
<option value="" disabled="disabled" selected="selected">Please select a batch</option>
<option value="batch1">Batch1</option>
<option value="batch2">Batch2</option>
<option value="batch3">Batch3</option>
<option value="batch4">Batch4</option>
<option value="batch5">Batch5</option>
</select>
</p>
<br/>
<p>
<label for="binpfile">Batch Input File</label>
<input type="file" id="binpfile" accept=".txt" name="binpfile"/>
</p>
<br/>
<p>
<label for="emailid">E-Mail ID:</label>
<input type="text" id="emailid" name="emailid"><br>
</p>
<br/>
<p class="formfield">
<label for="bparam">Batch Parameters:</label>
<textarea rows="4" cols="50" id="bparam" name="bparam">Enter batch param here...</textarea>
</p>
<br/>
<div class="wrapper">
<input type="submit" value="Submit">
</div>
</form>
</body>
</html>
app.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
路由/ index.js
var methods = require('../methods/method');
/* GET batch form entry */
router.get('/batch', function(req, res) {
methods.processbatch(req, res);
});
方法/ method.js
var multer = require('multer');
var upload = multer({ dest: './uploads/' });
var exports = module.exports = {};
exports.processbatch = function(req, resp) {
var bname = req.query['batch'];
var fname = req.query['binpfile'];
var emailid = req.query['emailid'];
var bparam = req.query['bparam'];
upload.single(fname);
upload.array(fname);
upload.fields(fname);
console.log(req);
console.log ('User entered ' + bname + ' ' + fname + ' ' + emailid + ' ' + bparam);
console.dir(req.files);
};
我在我的方法中得到了所有参数,但是req.files记录为&#39; Undefined&#39;并且没有文件存储我的上传目录,其前景设置为777(R + W + X)?
答案 0 :(得分:0)
使用以下行
upload.single("binpfile")
并且无需使用
upload.single(fname);
upload.array(fname);
upload.fields(fname); the