0

I'm trying to use a simple file upload using angularjs, but the Upload and Close buttons are not working.

Here is my html:

<div>

    <div class="modal-header">
        <h3 class="modal-title">File Attachment</h3>
    </div>

    <div class="modal-body">
        <input type="file" file-model="myFile" />
    </div>

    <div class="modal-footer">
        <div class="btn-toolbar pull-right" role="toolbar">
            <div class="btn-group" role="group" ng-controller="FileUploadController as fileUploadCtrl">
                <button class="btn btn-default" ng-click="FileUploadCtrl.uploadFile()">Upload</button>
            </div>
            <div class="btn-group" role="group">
                <button type="button" class="btn btn-default" ng-click="$close()">Close</button>
            </div>
        </div>
    </div>

</div>

In addition, I am getting an error in my factory even before I hit the browse button that states the following, but cannot find a solution for it on the web even though I see many questions about it.

[$injector:undef] Provider 'fileUpload' must return a value from $get factory method.

Here is my factory method:

.factory('fileUpload', ['$http', function ($http) {
                this.uploadFileToUrl = function (file, uploadUrl) {
                    var fd = new FormData();
                    fd.append('file', file);

                    $http.post(uploadUrl, fd, {
                        transformRequest: angular.identity,
                        headers: { 'Content-Type': undefined }
                    })

                    .success(function () {
                    })

                    .error(function () {
                    });
                }
            }])

Here is my directive:

.directive('fileModel', ['$parse', function ($parse) {
            return {
                restrict: 'A',
                link: function (scope, element, attrs) {
                    var model = $parse(attrs.fileModel);
                    var modelSetter = model.assign;

                    element.bind('change', function () {
                        scope.$apply(function () {
                            modelSetter(scope, element[0].files[0]);
                        });
                    });
                }
            };
        }])

Here is my js file function:

module.controller('FileUploadController', ['$scope', 'fileUpload', function ($scope, fileUpload)
    {
        $scope.uploadFile = function ()
        {
            var file = $scope.myFile;

            console.log('file is ');
            console.dir(file);

            var uploadUrl = "/fileUpload";

            fileUpload.uploadFileToUrl(file, uploadUrl);  
        };
    }])

Note that I need to use a factory and directive since this file attachment functionality will be used across multiple forms.

From what the error message says, it looks like I need to return a value from the factory method, but don't know what....

Can someone please tell me how I can accomplish the file uploading here and what I'm doing wrong?

sagesky36
  • 4,542
  • 19
  • 82
  • 130

1 Answers1

0

Use factory like this

 .factory('fileUpload', ['$http', function ($http) {
       return
       {
           uploadFileToUrl : function(file, uploadUrl){
           var fd = new FormData();
           fd.append('file', file);

           $http.post(uploadUrl, fd, {
              transformRequest: angular.identity,
              headers: {'Content-Type': undefined}
           })

           .success(function(){
           })

           .error(function(){
           });
        }
       }
     }]);

or if you want instead of factory you can use 'service'

.service('fileUpload', ['$http', function ($http) {
        this.uploadFileToUrl = function(file, uploadUrl){
           var fd = new FormData();
           fd.append('file', file);

           $http.post(uploadUrl, fd, {
              transformRequest: angular.identity,
              headers: {'Content-Type': undefined}
           })

           .success(function(){
           })

           .error(function(){
           });
        }
     }]);

for more detail you can check this link : AngularJS: Service vs provider vs factory

Community
  • 1
  • 1
agm92
  • 314
  • 1
  • 3
  • 11