64图像上传到Rails回形针

64图像上传到Rails回形针

本文介绍了如何将Base 64图像上传到Rails回形针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在互联网上尝试了上百万种不同的教程,以了解如何将Base64图像从我的iOS应用程序上传到我的rails应用程序.看来,无论我如何格式化请求,它都不会被接受.

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. It seems that no matter how I format the request it just won't get accepted.

有人能确切知道如何将Base64图像上传到回形针吗?

Does anyone know definitively how to upload a Base64 image to paperclip?

我尝试将参数作为JSON发送

I tried sending the param as JSON

{ "thumbnail_image": "base64_data..." }

我还尝试附加数据网址

{ "thumbnail_image": "data:image/jpeg;base64,alkwdjlaks..." }

我尝试发送带有和不带有数据网址的JSON对象

I tried sending a JSON object with and without data url

{ "thumbnail_image": { "filename": "thumbnail.jpg", "file_data": "base64_data...", "content_type": "image/jpeg" } }

我一直得到这些Paperclip::NoHandlerError,然后将大量数据转储到我的日志中.

I consistently get these Paperclip::NoHandlerErrors and then it dumps a giant blob of data into my log.

推荐答案

您的Base64字符串似乎很好.您随时可以在 此处

Your Base64 string seems to be fine. You can always check that here

所以问题可能出在Rails方面.检查您收到的字符串与发送的字符串完全相同.

So the problem is probably on the Rails side. Check that the string you receive is exactly the same like the one you are sending.

使用回形针 4.2.1 ,我设法以这种方式保存了Base64 GIF文件:

With Paperclip 4.2.1 I managed to save Base64 GIF file that way:

具有:

class Thing
    has_attached_file :image

和POST属性:

{
    "thumbnail_data:" "data:image/gif;base64,iVBORw0KGgo..."
}

您要做的就是找到合适的适配器并指定original_filename.因此,对于控制器而言:

All you have to do is to find proper adapter and specify 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使从3.5.0版保存base64变得更加容易.

AFAIK Paperclip made it easier to save base64 from version 3.5.0.

希望有帮助!

这篇关于如何将Base 64图像上传到Rails回形针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 20:55