javascript - 对象不支持 AngularJS 中的属性或方法 'success'

标签 javascript angularjs asp.net-mvc-4

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

我的 C# Controller 代码

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

模块.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.4 的 Angular 版本,则 success 方法已弃用。使用 then 来捕捉 promise 。

$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!");
                 });

关于javascript - 对象不支持 AngularJS 中的属性或方法 'success',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44653521/

相关文章:

javascript - 表单未检测下拉列表中动态选择/分配的值

angularjs - 如何在 AngularJS 中刷新/无效 $resource 缓存

css - 颜色代码未在同一行对齐

c# - 在 asp.net mvc 中使用 Razor 的 Css View 模型

c# - 从 SQL Server 表中选择 NULL 值

asp.net-mvc-4 - 在 MVC4 中使用 DotNetOpenAuth 的 LinkedIn 完整个人资料详细信息

尽管删除了 getter,Javascript 还是不允许我设置只有 getter 的属性

javascript - 获取 Thymeleaf ${pageContext.request.contextPath} 的语法是什么

javascript - 谁能告诉我,为什么我的 js 代码没有在 jsfiddle 上运行?

javascript - Angular JS 使用与其他行不同的控件将行添加到 ng-repeat 表中