1

I have a code that uploads documents in the controller, I wanted it to be moved to a service, so that other controllers can consume it.

"use strict";
angular.module('myApp')
.service('uploadDocumentService', ['Upload', function (Upload) {
    this.UploadDocument = function ($file, data) {

        Upload.upload({
            url: '/uploadDocuments',
            file: $file,
            data: data
        }).progress(function (evt) {
            var progressReport = {};
            progressReport.progressVisible = true;
            progressReport.percentage = Math.round(evt.loaded / evt.total * 100);
            return progressReport;
        }).success(function (data, status, headers, config) {
            var fileUploaded = {};
            fileUploaded.id = data.id;
            fileUploaded.name = data.fileName;
            return fileUploaded;
        });
    }

}]);

I am unable to capture the .progress event in my controller

 uploadDocumentService.UploadDocument($file, 'Path')
                .progress(function (progressReport) {
                  //Some code
                }).success(function (data) {
                  //Some code

            });

Keep getting the error Cannot read property 'progress' of undefined at m.$scope.uploadDocuments Any tips on how to solve this problem, do I need to register the progress event in the service?

2
  • Please confirm whether your controller is having the service - uploadDocumentService..angular.module('myApp').controller("MyCtrl", function($scope, uploadDocumentService) Commented Jul 18, 2016 at 5:39
  • i think uploadDocumentService param is missing in your controller function which is causing that undefined error.. Hope this works for you :) Commented Jul 18, 2016 at 5:40

3 Answers 3

1

Controller code

"use strict";
angular.module('myApp')
.controller('controller', ['$scope', '$http',  'Upload', 'uploadDocumentService', function ($scope, $http, Upload, uploadDocumentService) {
 $scope.uploadDocuments = function ($files) {
        $scope.progressVisible = false;
        for (var i = 0; i < $files.length; i++) {
            var $file = $files[i];
            uploadDocumentService.UploadDocument($file, 'path')
                .progress(function (evt) {
                    $scope.progressVisible = true;
                    $scope.percentage = Math.round(evt.loaded / evt.total * 100);

                }).success(function (data) {
                var fileUploaded = {};
                fileUploaded.id = data.id;
                fileUploaded.name = data.fileName;
                $scope.filesUploaded.push(fileUploaded);
                $scope.isFileUploaded = true;                   

            });
}]);
Sign up to request clarification or add additional context in comments.

Comments

1

A colleague pointed out the mistake, the fix is as below, return was missing in the statement Upload.upload

"use strict";
angular.module('myApp')
.service('uploadDocumentService', ['Upload', function (Upload) {
    this.UploadDocument = function ($file, data) {
        return Upload.upload({
            url: '/uploadDocuments',
            file: $file,
            data: data
        }).progress(function (evt) {

        }).success(function (data, status, headers, config) {
        });
    }

}]);

Comments

0

To achieve your expected result,add uploadDocumentService param in your controller function.

angular.module('myApp').controller("controller", function($scope, uploadDocumentService) 

4 Comments

@Rahul Ganguly, if my answer helped your question, please vote or mark it as answered :)
This is present in my controller,
@RahulGanguly, please share your controller code as well
thanks for the help, A colleague pointed out that I had missed on a return statement, the code in the service should have been like this return Upload.upload({ })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.