javascript - Trying to upload images using multer and get: read property 'files' of undefined -


hello i´m learning how upload images using express error:

typeerror: cannot read property 'files' of undefined @ saveimage (/users//express_test/controllers/image.js:51:37) @ create (/users//express_test/controllers/image.js:67:13)

my image.js file:

    var fs = require('fs'),     path = require('path');     create: function(req, res) {            var saveimage = function(req,res) {               var possible = 'abcdefghijklmnopqrstuvwxyz0123456789',               imgurl = '';               for(var i=0; < 6; i+=1) {                  imgurl += possible.charat(math.floor(math.random() *                            possible.length));               }                var temppath = req.files.photo.path;                ext =  path.extname(req.files.photo.name).tolowercase(),                targetpath = path.resolve('./public/upload/' + imgurl + ext);               if (ext === '.png' || ext === '.jpg' || ext === '.jpeg' || ext ===                '.gif') {                fs.rename(temppath, targetpath, function(err) {                    if (err) throw err;                    res.redirect('/images/' + imgurl);                });            } else {                fs.unlink(temppath, function () {                    if (err) throw err;                    res.json(500, {error: 'only image files allowed.'});                });              }        };         saveimage(); //guardar imagen 

my configure.js file:

    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')}).array('photo')); //para subir imagenes   app.use(methodoverride());   app.use(cookieparser('some-secret-value-here'));   routes(app);//moving routes routes folder.disponible hacer get,post,update      app.use('/public/', express.static(path.join(__dirname,           '../public')));//direccion de archivos estaticos .js y .css middleware  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(); //helper general para nuestro timestamp                         }             }       }).engine);     app.set('view engine', 'handlebars');    return app;    }; 

file index.handlebars

   <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="photo" id="photo">               </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">                     <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> 

thanks much!


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -