2017-05-02 3 views
-2

angularjs 파일 업로드에 오류가 발생했습니다. 여기Angularjs 파일 업로드 코드 오류

이 내 코드 :

html

내가 콘솔 로그에 오류가 점점 오전 : 간단한 단어에

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

시작 추가! 코드 예제를 넣어보십시오. ** [How to Ask] (http://stackoverflow.com/help/how-to-ask) ** –

+0

코드 그림을 게시하지 마십시오. 코드 자체를 게시하십시오! –

+0

아마도 localhost로 보낼 포트 번호를 알려 주기만하면됩니다. (예 : '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 
     };