Skip to content

Instantly share code, notes, and snippets.

@enyo
Last active December 10, 2015 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enyo/4502571 to your computer and use it in GitHub Desktop.
Save enyo/4502571 to your computer and use it in GitHub Desktop.
var path = require("path");
exports.media = function(req, res, next) {
var file = req.files.media_upload;
// code to handle file here:
// file should already be in the right location
// The path, this file has been saved at should be:
file.path;
// To get only the filename of this path:
var filename = path.basename(file.path);
Project.findById(req.params.id, function(err, project) {
// First make sure that media is actually an array, not undefined.
if (!project.media) { project.media = [ ]; }
// Now add the filename
project.media.push(filename);
// And save it
project.save(function(err) {
if (err) { return next(err); }
res.send("success");
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment