How to upload a Base64 image to Rails paperclip

后端 未结 2 1228
悲哀的现实
悲哀的现实 2020-12-06 13:58

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

2条回答
  •  自闭症患者
    2020-12-06 14:49

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

    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.

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

    Having:

    class Thing
        has_attached_file :image
    

    and POST attributes:

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

    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 made it easier to save base64 from version 3.5.0.

    Hope that helps!

提交回复
热议问题