這讓我瘋狂。我試圖搜索,但提供的所有解決方案都不起作用。 我想用multer創建一個簡單的文件上傳,但似乎無法讓它工作,因爲req.file似乎總是未定義的。req.file是undefined multer
任何想法?完全困難..沒有任何工作,它只是回來未定義!
app.js
var path = require('path'),
routes = require('./routes'),
exphbs = require('express-handlebars'),
express = require('express'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
morgan = require('morgan'),
methodOverride = require('method-override'),
errorHandler = require('errorhandler');
moment = require('moment');
module.exports = function(app) {
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({'extended':true}));
app.use(bodyParser.json());
//app.use(multer({ dest: path.join(__dirname, 'public/upload/temp')}));
app.use(methodOverride());
app.use(cookieParser('some-secret-value-here'));
routes(app);//moving the routes to routes folder.
app.use('/public/', express.static(path.join(__dirname,'../public')));
if ('development' === app.get('env')) {
app.use(errorHandler());
}
app.engine('handlebars', exphbs.create({
defaultLayout: 'main',
layoutsDir: app.get('views') + '/layouts',
partialsDir: [app.get('views') + '/partials'],
helpers: {
timeago: function(timestamp) {
return moment(timestamp).startOf('minute').fromNow();
}
}
}).engine);
app.set('view engine', 'handlebars');
return app;
};
這裏是我的HTML:
<form method="post" action="/images" enctype="multipart/form-data">
<div class="panel-body form-horizontal">
<div class="form-group col-md-12">
<label class="col-sm-2 control-label" for="file">Browse:</label>
<div class="col-md-10">
<input class="form-control" type="file" name="file" id="file">
</div>
</div>
<div class="form-group col-md-12">
<label class="col-md-2 control-label" for="title">Title:</label>
<div class="col-md-10">
<input class="form-control" type="text" name="title">
</div>
</div>
<div class="form-group col-md-12">
<label class="col-md-2 control-label" for="description">Description:</label>
<div class="col-md-10">
<textarea class="form-control" name="description" rows="2"></textarea>
</div>
</div>
<div class="form-group col-md-12">
<div class="col-md-12 text-right">
<button type="submit" id="login-btn" class="btn btn-success" type="button">
<i class="fa fa-cloud-upload "></i> Upload Image</button>
</div>
</div>
</div>
</form>
路線:
var express = require('express'),
path = require('path'),
router = express.Router(),
home = require('../controllers/home'),
image = require('../controllers/image'),
multer = require('multer');
var upload = multer({ dest: path.join(__dirname, 'public/upload/temp')});
module.exports = function(app) {
router.get('/', home.index);
router.get('/images/:image_id', image.index);
router.post('/images', upload.single('file'), image.create);
router.post('/images/:image_id/like', image.like);
router.post('/images/:image_id/comment', image.comment);
app.use(router);
};
控制器:
create: function(req, res) {
console.log(req.file);
}
又一個音符...... Req.body完全是空的......我把它看作是: 體{}
我想我已經取得了一些進展。我看到了一些文件和內容,但它仍然表示它是未定義的? 體是:{title: 'fdsfdsa',說明: 'fdsafdsa'}, originalMethod: 'POST', 成功祕訣: '一些祕密的價值 - 在這裏', 餅乾:{}, signedCookies:{}, 路線: 路線{ 路徑: '/圖片', 堆棧:[[對象],[對象]], 方法:{交:真}}, 讀:[功能], 文件: {字段名: '文件', ORIGINALNAME: 'Chrysanthemum2.jpg', 編碼: '7位', mime類型: '圖像/ JPEG', 目的地: 'C:\ testapp \服務器\公共\上傳\ TEMP', 文件名: 'fd17765e4cd03268db6757b8cfb21649', 路徑: 'C:\ testapp \服務器\公用\上傳\ TEMP \ fd17765e4cd03268db6757b8c b21649', 大小:15973}} ypeError:未定義
我試過兩種都沒有定義。任何人? – user1655940
@ user1655940 - 看看我添加到我的答案中,沒有爲調用'image.create()'的路由安裝中間件。 – jfriend00
愚蠢的錯誤。剛做了更新和測試。仍未定義。 – user1655940