1

I am using multer module to upload files to my application with a multiform-data enctype on my form. However, I have tried all possible solutions I have found to upload the file in vain. I am receiving always a "Cannot read property path of undefined or file of undefined. Here below is my view, controller and config files, anyone can help point was it wrong?

View

<form method="post" action="/images" enctype="multipart/form-data">
    <div class="panel-body form-horizontal">
        <div class="form-group col-md-12">
            <label for="file" class="col-sm-2 control-label">Browse:</label>
            <div class="col-md-10">
                <input type="file" name="file" id="file" class="form-control">
            </div>
        </div>

controller:

var tempPath = req.files.path;
var ext = path.extname(req.files.name).toLowerCase();
var finalPath = path.resolve('./public/upload' + imageUrl + ext);

config file:

var path = require('path');
var express = require('express');
var routes = require('./routes'); //routes for GET, POST...requests
var exphbs = require('express-handlebars'); //templating engine
//var bodyParser = require('body-parser'); //form submission request are           accessible with req.body
var cookieParser = require('cookie-parser'); //cookies to be send and received
var morgan = require('morgan'); //module for logging - used in debugging
var methodOverride = require('method-override'); //for older browser to fake    REST verbs
var errorHandler = require('errorhandler'); //handles error through the middleware
var moment = require('moment'); //npm module to handle dates formating
var multer = require('multer'); //to handle file uploading

module.exports = function(app) {

app.use(morgan('dev'));
//app.use(multer({dest: path.join(__dirname, 'public/upload/temp')}));     //proper use of multer
app.use(multer({ dest: path.join(__dirname,'public/upload/temp')}).any());

app.use(methodOverride());
app.use(cookieParser('IciCestParis'));

routes(app); //moving the routes to route folder

app.use('/public/', express.static(path.join(__dirname,'../public')));
Celaro
  • 196
  • 1
  • 7
  • 19
  • Anyone to point me to the right direction please? I still have not find out why I am getting such error. – Celaro Mar 20 '17 at 01:31
  • I have the same issue. Did you find the solution or any workaround for this ? – hemanik Sep 18 '17 at 06:17
  • The multer module has been updated and its usage has changed. Please see the following Stackoverlow article for corrections: http://stackoverflow.com/questions/31496100/cannot-app-usemulter-requires-middleware-function-error – Adam Heaton Apr 27 '18 at 10:55

0 Answers0