简体   繁体   中英

Convert picture to binary BASE64

How do I convert a picture to binary BASE64 and send it by post to another API to be saved?

It looks something like:

  1. User imports a photo through the HTML form and it transforms it to binary BASE64 code:

     base64_encode = Base64.encode64(img_from_fild_form) 
  2. The data is sent to another API (media):

     options = { :auth_key=>01qw6, :post_id=>20, :base64_encode=>base64_encode } HTTParty.post('localhost:9000/save_image', options) 

if the input is from a rails form then it probably as easy as:

Base64.encode64(params[:key_for_file].read)

the uploaded file is likely an instance of ActionDispatch::Http::UploadedFile

It should be as easy as:

require 'base64'
base64_encoded = Base64.encode64(img_from_file_form)

Hope that helps. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM