【问题标题】:405 Error $http post json file to a folder using IIS405 错误 $http 使用 IIS 将 json 文件发布到文件夹
【发布时间】:2017-02-02 12:17:45
【问题描述】:

我的代码看起来像

     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;

             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();
           fd.append('file', file);

           $http.post(uploadUrl, fd, {
             transformRequest: angular.identity,
             headers: {
               'Content-Type': 'application/json'
             }
           })

           .success(function() {})

           .error(function() {});
         }
       }
     ]);

     myApp.controller('myCtrl', ['$scope', 'fileUpload',
       function($scope, fileUpload) {
         $scope.uploadFile = function() {
           var file = $scope.myFile;

           console.log('file is ');
           console.dir(file);

           var uploadUrl = "http://localhost:22729/Data/";
           fileUpload.uploadFileToUrl(file, uploadUrl);
         };
       }
     ]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.min.js"></script>

<body ng-app="myApp">

  <div ng-controller="myCtrl">
    <input type="file" file-model="myFile" />
    <button ng-click="uploadFile()">upload me</button>
  </div>
</body>

请注意,我已经尝试了所有可用的替代方法。

web.config 如下所示

<staticContent>
    <mimeMap fileExtension=".json" mimeType="application/json" />
  </staticContent>

  <httpProtocol>
    <customHeaders>
      <add name="Access-Control-Allow-Origin" value="*"/>
      <add name="Access-Control-Allow-Headers" value="Content-Type" />
      <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
    </customHeaders>
  </httpProtocol>

我尝试了很多替代方案,但仍然面临这个问题。

我也想知道如何上传其他文件类型。

【问题讨论】:

    标签: angularjs json http post file-upload


    【解决方案1】:

    看起来它可能正在通过静态文件处理程序运行默认文档模块。如果是这样,那将是自动 405。

    您是否希望此上传运行您的服务器代码来捕获上传并将其保存在服务器上?如果是这样,您将不得不重新排列模块和静态文件处理程序,并尝试让默认文档模块不碍事

    【讨论】:

    • 我希望文件只上传到本地文件夹
    • 我将引用另一个得到相同答案的线程:stackoverflow.com/questions/8232033/… 服务器端必须有某种形式的代码来捕获请求并将文件从请求正文复制到所需的位置。
    猜你喜欢
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 1970-01-01
    相关资源
    最近更新 更多