上传`XML`文件不按预期工作

时间:2015-09-03 10:49:53

标签: angularjs angular-resource

我需要使用xml angularresource文件上传到服务器。我正在尝试,但收到错误Uncaught TypeError: server.uploadXML.post is not a function

任何人都纠正我或告诉我正确的处理方法吗? 要执行此过程,我将controllerdirectivehtml一起使用。

我很新,请在我误会的地方指导我。

这是我的控制器功能://我从指令

调用
$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 );

                    }

                }} ); 

            }

这是我的指示:

//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);

这是我的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>

我使用server.js文件上传文件,这是server.js文件:

(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')


            }

        }]);

})();

1 个答案:

答案 0 :(得分:1)

server.js文件中,当您执行

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

$resource使用以下方法返回:

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

请参阅角度documentation

所以server.uploadXML.post确实没有定义(这就是你得到错误的原因)。尝试使用server.uploadXML.save代替http POST方法。