3gwebtrain
3gwebtrain

Reputation: 15299

Uploading a `XML` file not working as per expected

I required to upload a xml file in to server using the angular resource. i am trying but getting an error as Uncaught TypeError: server.uploadXML.post is not a function

any one correct me or show me the correct way to handle this? to do this process, I am using controller and directive with my html.

I am very new, please guide me wherever I made mistaken.

here is my controller function : //i am calling from directive

$scope.uploadFile = function ( newFile, id ) {

                 var fileData = new FormData();
                 fileData.append('file', newFile[0], newFile[0].name);
                // console.log("new file", "$scope.packageId", $scope.packageId, "$scope.contractorId", $scope.contractorId, newFile, info );

                server.uploadXML.post({

                    packageId: $scope.packageId, 
                    contractorId : $scope.contractorId,
                    contractId : id

                }, { save: {
                    type: 'POST',
                    data: fileData,
                    cache: false,
                    dataType: 'json',
                    processData: false, 
                    contentType: "application/octet-stream", 
                    success : function ( data ) {

                    },
                    error : function ( error ) {

                        cosole.log( error );

                    }

                }} ); 

            }

Here is my directive:

//file upload handled here

var uploadFileDirective = function () {

    return {

        restrict : 'C',

        scope : {

            info:"=",
            upload:"="

        },

        link:function ( scope, element, attrs ) {

            element.on('change', function ( event ) {

                var files = event.target.files;
                scope.upload(files, scope.info ); //calling controller

            })
        }

    }

}

angular.module("tcpApp")
.directive("uploadFileDirective", uploadFileDirective);

Here is my HTML :

<div class="row row3">
                            <div class="cell">
                                <a ng-href="">Contract Details</a>
                                <span class="fileUpload">
                                    Upload Report 
                                    <!-- hanlded by directive by class name  -->
                                    <input info="contractor.Id" upload="uploadFile" class="uploadField upload-file-directive" type="file" />
                                </span>
                            </div>
                        </div>

                    </div>

I am using my server.js file to upload my file, here is the server.js file:

(function () {

    "use strict";

    angular
        .module("tcpApp")
        .factory("server", ['$resource', function ($resource) {

            var base = 'http://azvsptcsdev02:678/_vti_bin/CPMD.WEBSERVICE/ProjectInfoService.svc/';

            return {

                subPage : $resource( base + 'GetSubProjects/:id'),

                splash : $resource( base + 'GetProjectDetails'),

                projectSummary : $resource( base  + 'GetProjectByID/:id', {id : '@id'}), //at the top on the summary page

                contractorsList : $resource( base + 'Contracts/:projectId/:subProjectId/:phaseId/:disciplineId'), //left side list of contractos

                contractorInfo : $resource( base + 'Projects/:projectId/:subProjectId/:contractId/:disciplineId/:staticId'), //summary page getting contractor info

                allProjectList : $resource( base + 'GetAllProjects'), //getting all project for all project page

                allSubProjects : $resource( base + 'GetAllSubProjects/:packageId'),

                allContracts : $resource( base + 'AllContracts/:packageId/:contractorId'),

                uploadXML   : $resource( base + 'UploadContract/:packageId/:contractorId/:contractId')


            }

        }]);

})();

Upvotes: 1

Views: 637

Answers (1)

tombarti
tombarti

Reputation: 426

In your server.js file, when you do

uploadXML   : $resource( base + 'UploadContract/:packageId/:contractorId/:contractId')

$resource returns on abject with the following methods available:

{ 'get':    {method:'GET'},
  'save':   {method:'POST'},
  'query':  {method:'GET', isArray:true},
  'remove': {method:'DELETE'},
  'delete': {method:'DELETE'} };

See the angular documentation.

So server.uploadXML.post is indeed not defined (that's why you get the error). Try using server.uploadXML.save instead which corresponds to the http POST method.

Upvotes: 1

Related Questions