0

I have created till now a directive with link and controller whose is sending at the moment when is inserted into the input file, HTTP post to API in nodeJS. And the problem is in directive:

Generally, I want to know: Did I do it right?

The error which is print out in console log :

TypeError: $http(...).success is not a function [Więcej informacji]

and

Possibly unhandled rejection: {"data":"Multipart: Boundary not found Error: Multipart: Boundary not found\n at new

HTML:

<input type="file" file="file" required />

Directive:

App.directive('file', function () {
    return {
        scope: {
            file: '='
        },
        link: function (scope, el, attrs) {
            el.bind('change', function (event) { 
                var file = event.target.files[0];
                scope.file = file ? file : undefined;
                scope.$apply(); 
                scope.check();
            });
        },
        controller: function($scope, $http) {

            $scope.check = () => { 
                if($scope.file) {
                    $http({
                        method: 'POST',
                        url: '/card/upload-image',
                        headers: {
                            'Content-Type': 'multipart/form-data'
                        },
                        data: {
                            upload: $scope.file
                        },
                        transformRequest: function (data, headersGetter) {
                            var formData = new FormData();
                            angular.forEach(data, function (value, key) {
                                formData.append(key, value);
                            });

                            var headers = headersGetter();
                            delete headers['Content-Type'];

                            return formData;
                        }
                    })
                    .success(function (data) { 
                        console.log(data)
                    })
                    .error(function (data, status) {
                        console.log(data, status)
                    });                 
                }

            }
        } 
    };
});
Mika Sundland
  • 14,170
  • 16
  • 31
  • 44

0 Answers0