问题描述
我真的很抱歉,如果我把一些东西出来,完全是愚蠢的,但我已经检查并重新检查了很多次,文件上传功能只是没有在这里工作。我做了一个超级最小的应用程序来表示。只需生成一个最新版本的最新版本(3.4.7),并添加了最少可以进行文件上传工作。这是我的app.js文件
/ **
*模块依赖关系。
* /
var express = require('express');
var http = require('http');
var path = require('path');
var app = express();
//所有环境
app.set('port',process.env.PORT || 3000);
app.set('views',path.join(__ dirname,'views'));
app.set('view engine','jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__ dirname,'public')));
//仅开发
if('development'== app.get('env')){
app.use(express.errorHandler());
}
app.get('/ tasks',function(req,res){
res.render('form');
});
app.post('/ tasks',function(req,res){
console.log(req.files);
res.send('ok');
});
http.createServer(app).listen(app.get('port'),function(){
console.log('Express server listening on port'+ app.get 'port'));
});
exports = module.exports = app;
这是我的form.jade视图文件:
$ $ $ $ $
标题任务上传
body
form(action ='/ tasks',方法='post',enctype ='multipart / form-data')
input(name ='task',type ='file')
input(type ='submit')
每次我尝试上传一个文件, req.files
注销未定义
。有谁能救我脱离这个问题?
在您的app.js中添加以下内容
app.configure(function(){
app.use(express.methodOverride());
app.use(express.bodyParser({keepExtensions:true,uploadDir:path .join(__ dirname,'/ files'}));
});
然后尝试访问如下;
req.files.task
建议不要使用bodyParser,而是简单地定义所需的处理类型,在您的情况下,您可以启动文件上传如下
app.configure(function(){
app.use(express.methodOverride());
app.use(express.multipart());
});
您可以阅读关于在以下链接中使用bodyParser()不是一个好主意的原因。
I really apologize if I'm leaving something out and am totally stupid, but I've checked and checked over again a number of times, and the file upload functionality is just not working over here. I made a super minimal app to demonstate. Just generated a new express app with the most up-to-date version (3.4.7) and added the least i could to make a file upload work.
Here's my app.js file
/**
* Module dependencies.
*/
var express = require('express');
var http = require('http');
var path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/tasks', function(req, res) {
res.render('form');
});
app.post('/tasks', function(req, res) {
console.log(req.files);
res.send('ok');
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
exports = module.exports = app;
And here's my form.jade view file:
doctype html
html
head
title Task Upload
body
form(action='/tasks', method='post', enctype='multipart/form-data')
input(name='task', type='file')
input(type='submit')
Everytime I try to upload a file, req.files
logs out undefined
. Can anyone save me out from this problem?
Add the following in your app.js
app.configure(function(){
app.use(express.methodOverride());
app.use(express.bodyParser({keepExtensions:true,uploadDir:path.join(__dirname,'/files'}));
});
And then try to access as follows;
req.files.task
It is recommended not to use bodyParser, but to simply define the type of handling you want. In your case since its file uploading, you can enable it as follows
app.configure(function(){
app.use(express.methodOverride());
app.use(express.multipart());
});
You can read about why using bodyParser() is not a good idea in the following link.
http://andrewkelley.me/post/do-not-use-bodyparser-with-express-js.html
这篇关于Express无法上传文件,req.files未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!