2013-08-31 2 views
5

저는 Paperclip gem을 사용하여 AngularJS/Rails로 파일 업로드를 구현하려고합니다. 지시어로 파일 입력 문제를 해결했습니다. 이제 게시물의 다른 데이터로 이미지를 보내고 싶지만 이미지 데이터는 전송되지 않습니다.AngularJS/Rails Paperclip 파일 업로드

HTML :

<form name="PostForm" ng-submit="submit()" novalidate> 
    <input type="text" ng-model="post.title"> 
    <input type="file" file-upload /> 
    <textarea ng-model="post.content"></textarea> 
</form> 

내 컨트롤러 :

$scope.create = function() { 

    function success(response) { 
     console.log("Success", response) 
     $location.path("posts"); 
    } 

    function failure(response) { 
     console.log("Failure", response); 
    } 

    if ($routeParams.id) 
     Post.update($scope.post, success, failure); 
    else 
     Post.create($scope.post, success, failure); 
    } 

$scope.$on("fileSelected", function (event, args) { 
    $scope.$apply(function() { 
     $scope.post.image = args.file; 
    }); 

내 모델 :

class Post < ActiveRecord::Base 
    attr_accessible :content, :title, :image_file_name, :image_content_type, :image_file_size, :image_updated_at 

    belongs_to :user 
    has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png" 
end 

하지만 서버 측에 보낼 때, 요청이 그런 식이다 :

{ 
    "content":"Hey", 
    "created_at":"2013-08-31T17:54:32Z", 
    "id":17, 
    "image_content_type":null, 
    "image_file_name":null, 
    "image_file_size":null, 
    "image_updated_at":null, 
    "title":"Image", 
    "updated_at":"2013-08-31T17:54:32Z", 
    "user_id":4 
} 

그래서 이미지를 보존하는 데이터는 서버로 전송되지 않으므로 그 방법에 대한 정보를 얻으시겠습니까? 지금 진행

+0

헤이 조,이 힘 도움은 https : //gist.github 데이터를 전송 .com/vajapravin/48059fd9d64bb42f012f513cebd391ea – vajapravin

답변

0

, 난 형태의 데이터의 콘텐츠 형식을 통해 파일 데이터를 보내는 데 성공, 문제가 레일 컨트롤러에 지금, 오류가 시작됩니다 : 여기

undefined method `stingify_keys` 

내가하는 데 사용하는 코드입니다

$http({ 
     method: 'POST', 
     url: url, 
     headers: { 'Content-Type': false }, 
     transformRequest: function (data) { 
      var formData = new FormData(); 
      formData.append("post", angular.toJson(data.post)); 
      formData.append("image", data.image); 
      return (formData); 
     }, 
     data: { post: $scope.post, image: $scope.image} 
    }). 
success(function (data, status, headers, config) { 
    alert("success!"); 
}). 
error(function (data, status, headers, config) { 
    alert("failed!"); 
}); 

그리고 그 레일에 전송되는 데이터의 :

-----------------------------2122519893511 
Content-Disposition: form-data; name="post" {"title":"sdsdsd","content":"sdsd"} 
-----------------------------2122519893511 
Content-Disposition: form-data; name="image"; filename="dad.jpg" Content-Type: image/jpeg [image-data] 
-----------------------------2122519893511-- 
+0

안녕하세요 @ jeo-bow, 레일이있는 각도로 클립으로 이미지를 업로드 할 수 있습니까? –

관련 문제