【问题标题】:Upload excel files angularjs and get SOA APIs issues上传 excel 文件 angularjs 并获取 SOA API 问题
【发布时间】:2015-03-04 12:22:53
【问题描述】:

今天,我遇到了在 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/设置 public void fileUpload([FormData] UploadExcel value, int id) { }

我的问题是

【问题讨论】:

    标签: angularjs exception file-upload


    【解决方案1】:

    我想这可能对你有帮助。按照这个步骤

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

    【讨论】:

      猜你喜欢
      • 2017-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多