2016-09-24 58 views
0
文件夾

我的代碼看起來像

 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>

請注意,我已經嘗試了所有可用的替代品。 ErrorImage

的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> 

我試過很多備選方案,仍然面臨這個問題。

另外我想知道其他文件類型是如何上傳的。

回答

0

它看起來好像它可能通過靜態文件處理程序運行默認文檔模塊。如果是的話,那將是一個自動405。

您是否期待這種上傳運行您的服務器代碼來捕獲上傳並將其保存在服務器上?如果是這樣,您將不得不重新排列模塊和靜態文件處理程序,並嘗試獲取默認文檔模塊

+0

我想要文件只上傳到本地文件夾 – Aravind

+0

我會引用另一個線程結束了相同的答案:http://stackoverflow.com/questions/8232033/use-http-post-to-send-file-to-iis-7-5-virtual-directory必須有某種形式的代碼在服務器端捕獲請求並將文件從請求主體複製到所需的位置。 – milope