[英]How to upload a Base 64 image to Rails paperclip
I've tried a million different tutorials on the internet for how to upload a Base64 image from my iOS application to my rails app. 我已经在互联网上尝试了一百万种不同的教程,用于如何将我的iOS应用程序中的Base64图像上传到我的rails应用程序。 It seems that no matter how I format the request it just won't get accepted.
似乎无论我如何格式化请求它都不会被接受。
Does anyone know definitively how to upload a Base64 image to paperclip? 有谁知道如何将Base64图像上传到回形针?
I tried sending the param as JSON 我尝试将参数作为JSON发送
{ "thumbnail_image": "base64_data..." }
I also tried appending the data url 我也尝试附加数据网址
{ "thumbnail_image": "data:image/jpeg;base64,alkwdjlaks..." }
I tried sending a JSON object with and without data url 我尝试使用和不使用数据URL发送JSON对象
{ "thumbnail_image": { "filename": "thumbnail.jpg", "file_data": "base64_data...", "content_type": "image/jpeg" } }
I consistently get these Paperclip::NoHandlerError
s and then it dumps a giant blob of data into my log. 我一直得到这些
Paperclip::NoHandlerError
,然后它将Paperclip::NoHandlerError
数据转储到我的日志中。
Your Base64 string seems to be fine. 您的Base64字符串似乎没问题。 You can always check that here
你总是可以在这里查看
So the problem is probably on the Rails side. 所以问题可能在Rails方面。 Check that the string you receive is exactly the same like the one you are sending.
检查您收到的字符串是否与您发送的字符串完全相同。
With Paperclip 4.2.1 I managed to save Base64 GIF file that way: 使用Paperclip 4.2.1我设法以这样的方式保存Base64 GIF文件:
Having: 有:
class Thing
has_attached_file :image
and POST attributes: 和POST属性:
{
"thumbnail_data:" "data:image/gif;base64,iVBORw0KGgo..."
}
All you have to do is to find proper adapter and specify original_filename. 您所要做的就是找到合适的适配器并指定original_filename。 So for controller that would be:
所以对于控制器来说:
def create
image = Paperclip.io_adapters.for(params[:thumbnail_data])
image.original_filename = "something.gif"
Thing.create!(image: image)
...
end
AFAIK Paperclip made it easier to save base64 from version 3.5.0. AFAIK Paperclip可以更轻松地从3.5.0版本中保存base64。
Hope that helps! 希望有所帮助!
This is how I have done it in the past and it is basically a brute force approach, not sure if paperclip has added better support in recent versions, but this should work 这就是我过去的做法,它基本上是一种蛮力的方法,不确定回形针是否在最近的版本中增加了更好的支持,但这应该工作
class FooBar < ActiveRecord::Base
has_attached_file :thumbnail_image
validates_attachment_content_type :thumbnail_image,
content_type: %w(image/jpeg image/jpg image/png image/gif),
message: "is not gif, png, jpg, or jpeg."
attr_accessor :base64_thumbnail_image
# call this explicitly from the controller or in an after_save callback
# after setting the base64_thumbnail_image attribute
def save_base64_thumbnail_image
if base64_thumbnail_image.present?
file_path = "tmp/foo_bar_thumbnail_image_#{self.id}.png"
File.open(file_path, 'wb') { |f| f.write(Base64.decode64(base64_thumbnail_image)) }
# set the paperclip attribute and let it do its thing
self.thumbnail_image = File.new(file_path, 'r')
end
end
end
# params should be base64_thumbnail_image, not thumbnail_image in this case
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.