node.js - Store an Image from registration page using NodeJS -


i using expressjs , nodejs . build registration page require user upload image. code in jade

.form-container h1 create user form(name="adduser",method="post",action="/adduser")   .col-xs-13     p username     input.form-control(type="text", name="username")   .col-xs-13     p email     input.form-control(type="text", name="useremail")   .col-xs-13     p upload image     input.form-control(type='file', name ="pic", accept='image/*')   .col-xs-13     p password     input#password.form-control(type='text', name='pass',  required='')   .col-xs-13     p confirm password     input#confirm_password.form-control(type='text',  required='' , onkeyup='checkpass(); return false;')     span#confirmmessage.confirmmessage   br   .form-group     button#mybtn.btn.btn-primary(type="submit") submit 

i have script down there doesnt matter right now. thing want save image in express project directory not sure how that. here code store other information in mongodb db , need save image now.

/* post add user service */ router.post('/adduser', function(req, res) {      // set our internal db variable     var db = req.db;      // our form values. these rely on "name" attributes     var username = req.body.username;     var useremail = req.body.useremail;     var userpass = req.body.pass;      var collection = db.get('users');      // submit db     collection.insert({         "username" : username,         "email" : useremail,         "password" : userpass     }, function (err, doc) {         if (err) {              res.send("there problem adding information database.");         }         else {              res.redirect("userlist");         }     });  }); 

this might you:

var express = require('express'), app = express(), formidable = require('formidable'), util = require('util'), fs   = require('fs-extra'), bodyparser=require('body-parser'), qt   = require('quickthumb'), path    = require('path');   var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/db');  var images = require('./model.js');    app.use(qt.static(__dirname + '/')); app.use(bodyparser()); app.set('view engine','ejs');   app.post('/upload',function (req, res){     var form = new formidable.incomingform();   var userobj = [];  form.parse(req, function(err, fields, files) {       });   form.on('field',function(name,value){       // input field values here...       console.log(name,value);//name gives input field name , value gives input value       userobj.push({name:value});   });     form.on('end', function(fields, files) {             //you can save data collection array            //images model             var img = new images();              var temp_path = this.openedfiles[x].path;            /* file name of uploaded file */             var file_name = this.openedfiles[x].name;             //console.log('file '+file_name);             img.size = this.openedfiles[x].size;             img.type = this.openedfiles[x].type;              /* location want copy uploaded file */             var new_location = 'uploads/';                //to copy file folder                      fs.copy(temp_path, new_location + file_name, function(err)       {                 if (err) {                 console.log(err);               }            });//fscopy          });//form end res.send('done!!');  });//post app.listen(3000); console.log('started server'); 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -