req.file是未定义的multer

这让我疯狂。 我试图搜索,但提供的所有解决方案都不起作用。 我正尝试使用multer创建一个简单的文件上传,但似乎无法使其工作,因为req.file似乎始终未定义。

有任何想法吗? 完全困难..没有任何工作,它只是回来未定义!

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是完全空的......我认为它是:body {}

我想我已经取得了一些进展。 我看到了一些文件和内容,但它仍然表示它是未定义的? body:{title:'fdsfdsa',description:'fdsafdsa'},originalMethod:'POST',secret:'some-secret-value-here',cookies:{},signedCookies:{},route:Route {path: '/ images',stack:[[Object],[Object]],methods:{post:true}},read:[Function],file:{fieldname:'file',originalname:'Chrysanthemum2.jpg',encoding :'7bit',mimetype:'image / jpeg',目标:'C: testapp server public upload temp',文件名:'fd17765e4cd03268db6757b8cfb21649',路径:'C: testapp server public upload temp fd17765e4cd03268db6757b8c b21649',size:15973}} ypeError:无法读取未定义的属性'文件'


如果你正在使用upload.single() (你的代码显示),那么结果将在req.file ,而不是在req.files 。 所以改变这个:

create: function(req, res) {        
    console.log(req.files);
}

对此:

create: function(req, res) {        
    console.log(req.file);
}

另外,如果这是您期望使用的这条路线:

router.post('/images', image.create);

然后,您没有为该路线安装upload()中间件。

链接地址: http://www.djcxy.com/p/5353.html

上一篇: req.file is undefined multer

下一篇: Send image and json data to server in a ionic application