javascript - How to upload a user profile photo with Angularjs to the mongodb database (user model) (mean stack)? -
i want able upload single photo user object profile photo. i've uploaded photos in past multiparty, i'm new angularjs , trying figure out how done.
here's how i've used multiparty in past:
model:
user.prototype.save = function(fields, files, cb){ var properties = object.keys(fields), self = this; properties.foreach(function(property){ self[property] = fields[property][0]; }); var oldphotos = this.photos, newphotos = movefiles(files, oldphotos.length, '/img/' + this._id); this.photos = oldphotos.concat(newphotos); this.location = {name:this.loc, lat:this.lat, lng:this.lng}; this.isvisible = (this.isvisible === 'true') ? true : false; user.collection.save(this, cb); }; module.exports = user; function movefiles(files, count, reldir){ var basedir = __dirname + '/../static', absdir = basedir + reldir; if(!fs.existssync(absdir)){fs.mkdirsync(absdir);} var tmpphotos = files.photos.map(function(photo, index){ if(!photo.size){return;} var ext = path.extname(photo.path), name = count + index + ext, abspath = absdir + '/' + name, relpath = reldir + '/' + name; fs.renamesync(photo.path, abspath); return relpath; });
controller:
exports.update = function(req, res){ var form = new mp.form(); form.parse(req, function(err, fields, files){ user.findbyid(res.locals.user._id, function(err, user){ user.save(fields, files, function(err, cb){ res.redirect('/profile'); }); }); }); };
view:
.form-group label(for='photos') photos input.form-control#photo(type='file', name='photos', multiple=true) .form-group
excuse "noobness", basic projects i've done far angularjs, i've mirrored controller .factory on client side. in server-side controller, instead of redirect, i've done res.send , post in router. however, i'm not quite sure how angularjs same affect work , upload photo..
thanks in advance. also, i'm using mongodb without mongooose. i'm using ngroute currenlty, not ui-router angularjs. basically, i'm trying figure out how use entire user.protype.save function in angularjs.. been working on forever.. uncle
check out ng-flow. it's nice extension based on flow.js library , should give need.
Comments
Post a Comment