0

I want to be able to upload a single photo to the user object as a profile photo. I've uploaded photos in the past with multiparty, but now I'm new to angularjs and am trying to figure out how this is done.

here's how i've used multiparty in the 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 my "noobness", but for basic projects i've done so far with angularjs, i've basically mirrored the controller with a .factory on the client side. In the server-side controller, instead of redirect, i've done a res.send and post in the router. However, I'm not quite sure how to do that with the angularjs to get this same affect to work and to upload a photo..

Thanks in advance. Also, I'm using mongoDB without mongooose. I'm also using ngroute currenlty, not UI-router with angularjs. Basically, i'm trying to figure out how to use this entire user.protype.save function in angularjs.. been working on this forever.. uncle

NoobSter
  • 1,150
  • 1
  • 16
  • 39

1 Answers1

0

Check out ng-flow. It's a nice extension based on the Flow.js library and should give you what you need.

ceebreenk
  • 1,031
  • 2
  • 14
  • 29