对象不支持AngularJS中的属性或方法“成功”

时间:2017-06-20 12:47:49

标签: javascript angularjs asp.net-mvc-4

我想使用AngularJS使用MVC4上传文件,我的文件使用以下代码成功上传到服务器但由于错误我无法获得确认。请检查我已完成的所有代码。

我的C#控制器代码

[HttpPost]
    public JsonResult SaveFiles(string description)
    {
        string Message, fileName, actualFileName;
        Message = fileName = actualFileName = string.Empty;
        bool flag = false;
        if (Request.Files != null)
        {
            var file = Request.Files[0];
            actualFileName = file.FileName;
            fileName = Guid.NewGuid() + Path.GetExtension(file.FileName);
            int size = file.ContentLength;
            try
            {
                string fPath = Server.MapPath("~/UploadedFiles/");
                DirectoryInfo d = new DirectoryInfo(fPath);
                if (!d.Exists)
                {
                    d.Create();
                }
                file.SaveAs(Path.Combine(Server.MapPath("~/UploadedFiles"), fileName));
                flag = true;
                Message = "success";
            }
            catch (Exception)
            {
                Message = "File upload failed! Please try again";
            }

        }
        return new JsonResult
        {
            Data = new
            {
                Message = Message,
                Status = flag
            }
        };
    }

Module.js

  var myApp = angular.module("myApp", []);

Controller.js

    angular.module('myApp').controller('fileController', ["$scope", "FileUploadService", function ($scope, FileUploadService) {
    //Save File
    $scope.SaveFile = function () {        
        FileUploadService.UploadFile($scope.SelectedFileForUpload
                                     , $scope.FileDescription
            ).then(function (d) {
                console.log(d);
                alert(d.Message);
                ClearForm();
            }, function (e) {
                alert(e);
            });        
    }

}])
.factory('FileUploadService', function ($http, $q) {
    var fac = {};
    fac.UploadFile = function (file, description) {
        var formData = new FormData();
        formData.append("file", file);
        formData.append("description", description);
        // debugger;
        var defer = $q.defer();
        try {
            debugger;
            $http.post("/Home/SaveFiles", formData,
                  {
                      withCredentials: true,
                      headers: { 'Content-Type': undefined },
                      transformRequest: angular.identity
                  }).success(function (data, status, headers, config) {
                      debugger;
                      console.log('success...');                      
                      defer.resolve(d);
                  }).error(function (data, status, header, config) {
                      debugger;
                      console.log('error...');                      
                      defer.reject("File Upload Failed!");
                  });

        } catch (e) {
            debugger;
            console.log(e);
            console.log(e.message);
        }
        return defer.promise;
    }
    return fac;
});

错误

**TypeError**: Object doesn't support property or method 'success'
   at fac.UploadFile (http://localhost:52240/myScript/Controller.js:92:13)
   at $scope.SaveFile (http://localhost:52240/myScript/Controller.js:52:9)
   at fn (Function code:2:138)
   at callback (http://localhost:52240/Scripts/angular.js:26994:17)
   at Scope.prototype.$eval (http://localhost:52240/Scripts/angular.js:18161:9)
   at Scope.prototype.$apply (http://localhost:52240/Scripts/angular.js:18261:13)
   at Anonymous function (http://localhost:52240/Scripts/angular.js:26999:17)
   at r.event.dispatch (http://localhost:52240/Scripts/jquery-3.1.1.min.js:3:10259)
   at q.handle (http://localhost:52240/Scripts/jquery-3.1.1.min.js:3:8269)

请帮忙......

提前致谢。

1 个答案:

答案 0 :(得分:9)

如果您使用的是高于1.4的角度版本,则不推荐使用success方法。使用then来捕捉承诺。

$http.post("/Home/SaveFiles", formData,
                  {
                      withCredentials: true,
                      headers: { 'Content-Type': undefined },
                      transformRequest: angular.identity
                  }).then(function (response) {
                      debugger;
                      console.log('success...');                      
                      defer.resolve(response.data);
                  })
                  .catch(function (response) {
                     debugger;
                     console.log('error...');                      
                     defer.reject("File Upload Failed!");
                 });