[英]AngularJS/Rails Paperclip file upload
I trying to implement a file upload with AngularJS/Rails using the Paperclip gem. 我尝试使用Paperclip gem实现使用AngularJS / Rails的文件上传。 I fix the file input problem with a directive.
我用指令修复了文件输入问题。 Now i want to send the image with the other data of the post but the image datas are not send.
现在我想发送带有帖子的其他数据的图像,但不发送图像数据。
HTML : 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>
My controller : 我的控制器:
$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;
});
My model : 我的模特:
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
But when i send it to the server side, the request is like that : 但是当我将它发送到服务器端时,请求是这样的:
{
"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
}
So, no data conserning the image are send to the server, any tips on how to do that ? 因此,没有任何数据保存到服务器,任何提示如何做到这一点?
I progress now, i succeed to send file data via form-data content-type, the problem is now in the rails controller, an error is launched : 我现在进步,我成功通过表单数据内容类型发送文件数据,问题现在在rails控制器中,启动错误:
undefined method `stingify_keys`
Here is the code i use to send the data : 这是我用来发送数据的代码:
$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!");
});
And that's the data which are send to rails : 那就是发送给rails的数据:
-----------------------------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--
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.