angular.module('mongochem.services')
    .service('mongochem.MoleculeFileUploadService', ['mongochem.girder.UploadService',
                                                     'mongochem.Molecule', 'mongochem.girder.Collection',
                                                     'mongochem.girder.Folder', '$state', '$log',
                                                     '$timeout', '$q',
        function(uploadService, Molecule, Collection, Folder,
                $state, $log, $timeout, $q) {

            this.upload = function(files) {
                // Make sure we have a collection
                Collection.query({text: 'Logs'}).$promise.catch(
                        function(error) {
                            $log.error(error);
                        })
               .then(function(data) {
                   let deferred = $q.defer();
                    if(data.length === 0) {
                        Collection.create({name: 'Logs'}).$promise.then(
                                function(data) {
                                    deferred.resolve(data._id);
                                },
                                function(error) {
                                    deferred.reject(error);
                                });
                    }
                    else {
                        $timeout(function() {
                            deferred.resolve(data[0]._id);
                        });
                    }

                    return deferred.promise;
                })
                // Now get the Private folder
                .then(function(parentId) {
                    return Folder.query({text: 'Private',
                            parentType: 'collection',
                            parentId: parentId
                        }).$promise.catch(function(error) {
                            $log.error(error);
                    });
                 })
                 // Now we can do the upload
                 .then(function(folderData) {
                     uploadService.uploadFile('folder', folderData[0]._id, files[0]).then(function(id) {
                         Molecule.getByInchiKey({moleculeId: $state.params.moleculeId})
                         .$promise.then(function(molecule) {
                             if (!('logs' in molecule)) {
                                 molecule.logs = [];
                             }

                             molecule.logs.push({_id: id});
                             molecule.$update();
                         }, function(error) {
                             $log.error(error);
                         });
                     },
                     function(error) {
                         $log.error(error);
                     });
             });
        };
    }]);

@@ -2026,24 +2026,25 @@
 lderData) %7B%0A
+%0A
             
@@ -2056,77 +2056,29 @@
     
-uploadService.uploadFile('folder', folderData%5B0%5D._id, files%5B0%5D).then(
+let updateMolecule = 
 func
@@ -2626,33 +2626,34 @@
                %7D
-,
+;%0A
 %0A               
@@ -2649,32 +2649,50 @@
                 
+ let handleError =
  function(error)
@@ -2752,34 +2752,313 @@
                %7D
-)
 ;
+%0A%0A                     for(let i=0; i%3Cfiles.length; i++) %7B%0A                         uploadService.uploadFile('folder',%0A                                 folderData%5B0%5D._id, files%5Bi%5D).then(%0A                                         updateMolecule, handleError);%0A                     %7D
 %0A             %7D)

Add support for multiple file upload