我是相當新的表達和使用express.bodyParser來上傳文件有問題。 bodyParser按照預期與req.body一起工作,所以它似乎被正確設置。我正在運行節點0.6.17並表達2.5.8。每當我嘗試訪問req.files時,它都是未定義的。有誰知道這個問題的原因是什麼?Express.js文件上傳
從app.js:
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.cookieParser());
app.use(express.session({secret: "string" }));
app.use(flash());
app.use(express.bodyParser());
app.use(expressValidator);
app.use(express.methodOverride());
app.use(express.static(__dirname + '/public'));
app.use(app.router);
});
從index.js
app.get('/product/add', function(req, res) {
res.render("add_products", {
title: "Add Products",
email: req.session.email || 'Sign In/Up',
error: req.flash('error') || []
});
});
app.post('/product/add', function(req, res) {
console.log(req.files) // prints undefined
var errors = generate_error_messages(req, 'product/add') || [];
if (errors.length > 0) {
var errors_string_array = messages(errors);
req.flash('error', errors_string_array);
res.redirect('/product/add');
} else {
ProductDatabase.save(req, function(err, docs) {
res.redirect('/');
});
}
});
add_products.jade
form(class='form-horizontal', method='post', action='/product/add')
fieldset
.control-group
label(class='control-label', for="title") Product Title
.controls
input(type="text", class="input-xlarge", name="title")
.control-group
label(class='control-label', for="description") Description
.controls
textarea(class="input-xlarge", name="description", rows="5")
.control-group
label(class='control-label', for='auction_length') Auction Length
.controls
select(name='auction_length')
option 1 day
option 2 days
option 5 days
.control-group
label(class='control-label', for="fileInput") Upload Image
.controls
input(class='input-file', name='fileInput', type='file')
.form-actions
input(type="submit", class="btn btn-primary") Sell Product
a.btn(href='/') Cancel
你有沒有考慮過用厲害? – ControlAltDel
是的,我試圖添加var form = new formidable.IncomingForm(); form.parse(req,function(err,fields,files){...}在帖子路徑中,但是程序從未運行過解析方法 – user1403493
Formidable被express使用 –