Friday, 17 March 2017

NodeJS - Multer module error - Cannot read property path of undefined

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')));



via Celaro

No comments:

Post a Comment