Chris Ebert Chris Ebert - 1 year ago 58
Javascript Question

Uploading photographs using MEAN.js

I’m using this tutorial, which is based on the MEAN stack.
I want to store photographs that users can upload with MongoDB on a server.

included: Angular directive to upload files


<div data-ng-controller="SpotsCreateController">
<form class="form-signin" data-ng-submit="create(picFile)" novalidate>
<label>Upload an image</label>
<input type="file" id="articleimage" ng-model="picFile" ng-file-select="" ng-file-change="generateThumb(picFile[0], $files)" multiple name="file" accept="image/*">
<img ng-show="picFile[0].dataUrl != null" ng-src="{{picFile[0].dataUrl}}" class="img-thumbnail" height="50" width="100">
<span class="progress" ng-show="picFile[0].progress >= 0">
<div style="width:{{picFile[0].progress}}%" ng-bind="picFile[0].progress + '%'" class="ng-binding"></div>
<span ng-show="picFile[0].result">Upload Successful</span>

<input type="submit" class="btn btn-lg btn-primary btn-block" ng-click="uploadPic(picFile)">

<div data-ng-show="error">
<strong data-ng-bind="error"></strong>


<div data-ng-controller="SpotsViewController">
<section data-ng-init="findOne()">
<img ng-src="data:image/jpeg;base64,{{spot.image}}" id="image-id" width="200" height="200"/>


var app = angular.module('newApp', ['ngAnimate', 'ngCookies', 'ngResource', 'ngRoute', 'ngSanitize', 'ngTouch', 'ui.bootstrap', 'users', 'spots']);


angular.module('spots').controller('SpotsCreateController', ['$scope', '$timeout', 'Authentication', 'Spots', '$location'
function($scope, $timeout, Authentication, Spots, $location) {
$scope.authentication = Authentication;
$scope.fileReaderSupported = window.FileReader !== null;

$scope.create = function(picFile) {
var spot = new Spots({
title: this.title,
description: this.description,
image: null
spot.$save(function(response) {
$location.path('spots/' + response._id);
}, function(errorResponse) {
$scope.error =;

$scope.doTimeout = function(file) {
$timeout( function() {
var fileReader = new FileReader();
fileReader.onload = function(e) {
$timeout(function() {
file.dataUrl =;
$scope.generateThumb = function(file) {
if (file) {
if ($scope.fileReaderSupported && file.type.indexOf('image') > -1) {


var mongoose = require('mongoose'),
Schema = mongoose.Schema;

var SpotSchema = new Schema({
image: {
type: String,
default: '',
required: false

mongoose.model('Spot', SpotSchema);


var multiparty = require('connect-multiparty'),
multipartyMiddleware = multiparty();

module.exports = function(app) {
.post(users.requiresLogin, multipartyMiddleware, spots.create);

.put(users.requiresLogin, spots.hasAuthorization, spots.update)
.delete(users.requiresLogin, spots.hasAuthorization, spots.delete);

app.param('spotId', spots.spotByID);


var mongoose = require('mongoose'),
fs = require('fs'),
Spot = mongoose.model('Spot');

exports.create = function(req, res) {
if (req.files.file) {
var file = req.files.file;

var spot = new Spot(req.body);
spot.creator = req.user;

fs.readFile(file.path, function (err,original_data) {
if (err) {
return res.status(400).send({
message: getErrorMessage(err)
var base64Image = original_data.toString('base64');
fs.unlink(file.path, function (err) {
if (err) {
console.log('failed to delete ' + file.path);
} else {
console.log('successfully deleted ' + file.path);
spot.image = base64Image; {
if (err) {
return res.status(400).send({
message: getErrorMessage(err)
} else {

What did I do wrong? Please consider that I want to limit the file sizes, and I thought using
is good start. The issue is that the photograph is not stored in the database because the controller doesn’t work with the rest.

Answer Source

What exactly is the problem you are experiencing, and at what step is it going wrong?

For Express backend apps I usually use multer middleware for handling file uploads. Also, I create separate routes/controllers for dealing with the files rather than trying to process them at the same time I'm saving the parent object. This allows me to separate the logic nicely and not worry about the parent object not being saved when the file upload fails. You could use the JS API for ng-file-upload to handle that in Angular.

Example routes in Express (we have a "club" with a "logo" image here):
  ensureAuthenticated, ensureAdmin,

Example controller methods:

let multer = require('multer');
module.exports = {

   * Upload logo
  save(req, res, next) {

    //Get club and file
    let club =;
    let file = req.file;

    club.logo = {
      data: file.buffer,
      mimeType: file.mimetype

      .then(() => {

   * Stream logo
  stream(req, res, next) {
    let club =;

   * Upload middleware
  upload(req, res, next) {

    //Create upload middleware
    let upload = multer({
      storage: multer.memoryStorage(),
      limits: {
        fileSize: 50000000

    //Use middleware
    upload(req, res, next);

So as you can see, it's quite simple with multer and all you really need is one route for uploading the logo, with two controller methods, one to register the multer middleware and process the file, and the other to save it to the MongoDB (in this case attached to the club in the request).

Just make sure that ng-file-upload uses the same field name for uploading the file as multer is expecting. In the above example that's "logo". If you're unsure, check in the request what your client app is sending to the server and make sure the server app is expecting the same field name.

Let me know if you have further trouble.