hakuna
hakuna

Reputation: 6701

Upload file using Angular $resource

I am trying to upload a file using angular $resource, i am able to hit the Api but the request doesn't have any file. I have tried the solution here as well: AngularJS: Upload files using $resource (solution)

My Controller:

                        var formData = new FormData();
                        formData.append('file', $scope.file);
                        appService.manualDataUpload(formData)
                            .then(function (response) {
                              //do something here
                            })
                            .catch(function (error) {
                                logger.error('An error occurred while uploading the file !', error);
                            });

My Factory service:

function manualDataUpload(formData) {
            var /** @type {angular.Resource} */
            manualDataUploadResource = $resource(serviceBase + '/ManualDataUpload', formData,
                {
                    save: {
                        method: 'POST',
                        transformRequest: angular.identity,
                        headers: {
                            'Content-Type': undefined
                        }
                    }
                });

            return manualDataUploadResource
                .save()
                .$promise;
        }

Upvotes: 1

Views: 894

Answers (1)

hakuna
hakuna

Reputation: 6701

Removing the formData from $resource and adding in the body worked.

function manualDataUpload(formData) {
            var /** @type {angular.Resource} */
            manualDataUploadResource = $resource(serviceBase + '/ManualDataUpload', {},
                {
                    save: {
                        method: 'POST',
                        transformRequest: angular.identity,
                        headers: {
                            'Content-Type': undefined
                        }
                    }
                });

            return manualDataUploadResource
                .save(formData)
                .$promise;
        }

Upvotes: 1

Related Questions