2016-02-05 133 views
-1

我試圖使用多次 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&nbsp;&nbsp;</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); 
}); 

methods/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日誌爲'未定義',沒有文件存儲我的上傳目錄其前提設置爲777(R + W + X)?

回答