2013-10-25 3 views
1

anglejs를 사용하여 파일을 업로드하고 json 객체를 nodejs 서버로 단일 요청으로 보냅니다. 그럼 난 불을 지르고에서이 CONSOLE.LOG를 얻을 :FormData에 파일 추가가 anglejs에서 작동하지 않습니다.

-----------------------------8791641017658 Content-Disposition: form-data; name="model" {"phone":"asasasa","description":"121212112","payment":"2121212121","shipping":"121221221","imageurl":"21212112","price":"212112212","status":"new"} -----------------------------8791641017658-- 

그래서, 이름 = "파일은"여기에 표시되지 않습니다.

그리고 // print out "{}"

이 도와주세요 nodejs 서버에서 CONSOLE.LOG (req.files), 감사합니다!

$scope.uploadFile = function(files) { 

        uploadsService.uploadFile(files).then(function(promise){ 

         $scope.code = promise.code(); 
         $scope.fileName = promise.fileName(); 
    }); 

    }; 

그리고보기에 :

$scope.files = []; 

     $scope.$on("fileSelected", function (event, args) { 

     $scope.$apply(function() {    
      //add the file object to the scope's files collection 
      $scope.files.push(args.file); 
      console.log($scope.files); 

     }); 
     }); 

    $scope.create = function() { 
     $scope.seller_submit = { 
      phone: this.phone, 
      description: this.description, 
      payment: this.payment, 
      shipping: this.shipping, 
      imageurl: this.imageurl, 
      price: this.price, 
      status: this.status 
     }; 

     $http({ 
      method: 'POST', 
      url: '/articles/'+ $routeParams.articleId, 

      headers: { 'Content-Type': undefined }, 

      transformRequest: function (data) { 
       var formData = new FormData(); 

       formData.append("model", angular.toJson(data.model)); 

       for (var i = 0; i < data.files; i++) { 

        formData.append("file" + i, data.files[i]); 
       } 
       return formData; 
      }, 

      data: { model: $scope.seller_submit, files: $scope.files } 

     }). 
     success(function (data, status, headers, config) { 
      alert("success!"); 
     }). 
     error(function (data, status, headers, config) { 
      alert("failed!"); 
     }); 

답변

3

당신은 당신의 컨트롤러에서 해당

myApp.service('uploadsService', function($http) { 

    var code = ''; 
    var fileName = ''; 


    this.uploadFile = function(files) { 


     var fd = new FormData(); 

     //Take the first selected file 
     fd.append("image", files[0]); 

     var promise = $http.post('/uploads/uploadFile.json', fd, { 
       withCredentials: true, 
       headers: {'Content-Type': undefined }, 
       transformRequest: angular.identity 
      }).then(function(response) { 

      code = response.data.code; 
      fileName = response.data.fileName; 

      return{ 
       code: function() { 
        return code; 
       }, 
       fileName: function() { 
        return fileName; 
       } 
      }; 
     }); 
     return promise; 
    }; 

    }); 

그래서 당신이 수에 대한 서비스를 만들 수있는 무언가 같이 할

<input type="file" onchange="angular.element(this).scope().uploadFile(this.files)" ng-model="image"> 

나를위한 작품 :)

+0

매력처럼 작동합니다. – JayPea

+0

하나의 파일 만 업로드 할 수 있습니까? – nam

관련 문제