上传 excel 个文件 angularjs 并获取 SOA API 问题

Upload excel files angularjs and get SOA APIs issues

今天,我在 API 调用中遇到了获取上传文件的问题。我对此一无所知。

谁能告诉我怎么解决?

我现有的AngularJs是

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.css" />
    <script src="https://code.angularjs.org/1.3.0-rc.2/angular.js"></script>
    <script>
        var myApp = angular.module('myApp', []);

        myApp.directive('fileModel', ['$parse',
          function ($parse) {
              return {
                  restrict: 'A',
                  link: function (scope, element, attrs) {
                      var model = $parse(attrs.fileModel);
                      var modelSetter = model.assign;
                      debugger;
                      element.bind('change', function () {
                          scope.$apply(function () {
                              modelSetter(scope, element[0].files[0]);
                          });
                      });
                  }
              };
          }
        ]);

        myApp.service('fileUpload', ['$http',
          function ($http) {
              this.uploadFileToUrl = function (file, uploadUrl) {
                  var fd = new FormData();
                  debugger;
                  fd.append('file', file);
                  $http.post(uploadUrl, fd, {
                      transformRequest: angular.identity,
                      headers: {
                          'Content-Type': undefined
                      }
                  })
                    .success(function (resp) {
                        debugger;
                     })
                    .error(function (resp) {
                        debugger;
                    });
              }
          }
        ]);

        myApp.controller('myCtrl', ['$scope', 'fileUpload',
          function ($scope, fileUpload) {
              debugger;
              $scope.uploadFile = function () {
                  var file = $scope.myFile;
                  debugger;
                  //alert(JSON.stringify($scope.myFile));
                  //console.log('file is ' + JSON.stringify(file));
                  var uploadUrl = "http://localhost:56110/Api/Setting/fileUpload/1";
                  fileUpload.uploadFileToUrl(file, uploadUrl);
              };

          }
        ]);
    </script>
</head>
<body>
    <div ng-controller="myCtrl">
        <input type="file" file-model="myFile" />
        <button ng-click="uploadFile()">upload me</button>
    </div>
</body>
</html>

Api代码是

// POST api/setting public void fileUpload([FormData] UploadExcel 值, int id) { }

我的问题是

我认为它可能对您有所帮助。按照这个步骤

  1. 需要从服务中删除成功和错误函数。
  2. 上传成功后需要在控制器方法中重新绑定您的服务。