2017-05-02 41 views
-2

我收到了angularjs文件上傳中的錯誤。Angularjs文件上傳代碼錯誤

這裏這是我的代碼:

html

我得到在控制檯日誌中的錯誤:

angular.js:10765 POST http://localhost/kites/upload/ 404 (Not Found) 
+0

歡迎您!把你的代碼示例,你想要的。並訪問鏈接** [如何問](http://stackoverflow.com/help/how-to-ask)** –

+0

不要張貼代碼的圖片。發佈代碼本身! –

+0

您可能只需告訴它將哪個端口號作爲本地主機發送。 (例如:'http:// localhost:9002/kites/upload'/) – DDelgro

回答

1

使用此代碼

簡單的話

在HTML中

-控制器添加以下代碼只

 <form name="upload" class="form" data-ng-submit="addFile()"> 
    <input type="file" name="file" multiple 
onchange="angular.element(this).scope().uploadedFile(this)" /> 
<button type="submit">Upload </button> 
</form> 

- 當你點擊「上傳文件按鈕」調用此函數。它會上傳文件。你可以安慰它。

$scope.uploadedFile = function(element) { 
$scope.$apply(function($scope) { 
    $scope.files = element.files;   
}); 
    // console.log($scope.files) 
    // uploaded file is in $scope.files 

} 

添加更多的在控制器 - 下面的代碼添加到函數。當您點擊使用「點擊api(POST)」的按鈕時,會調用此函數。它會發送文件(上傳的)和表單數據到後端。

var url = "https://192.3.3.22/api/vi/userapi/reporttojson" 
// use can put you api in 'url' variable 
     var files=$scope.files; 

     for (var i = 0; i < files.length; i++) 
     { 
      var fd = new FormData(); 
      angular.forEach(files,function(file){ 
      fd.append('file',file); 
      }); 
      var data ={ 
       msg : message, 
       sub : sub, 
       sendMail: sendMail, 
       selectUsersAcknowledge:false 
      }; 

      fd.append("data", JSON.stringify(data)); 
       $http.post(url, fd, { 
       withCredentials : false, 
       headers : { 
       'Content-Type' : undefined 
       }, 
      transformRequest : angular.identity 
      }).success(function(data) 
      { 
        toastr.success("Notification sent successfully","",{timeOut: 2000}); 
        $scope.removereport() 
        $timeout(function() { 
        location.reload(); 
       }, 1000); 

      }).error(function(data) 
      { 
       toastr.success("Error in Sending Notification","",{timeOut: 2000}); 
       $scope.removereport() 
      }); 
     } 

在這種情況下..我添加下面的代碼爲形式的數據

var data ={ 
      msg : message, 
      sub : sub, 
      sendMail: sendMail, 
      selectUsersAcknowledge:false 
     };