1

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;
        }

1 Answer 1

1

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;
        }
Sign up to request clarification or add additional context in comments.

Comments

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.