Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

json - 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. 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?

I tried sending the param as 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

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...