简体   繁体   English

将图片转换为二进制BASE64

[英]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? 如何将图片转换为二进制BASE64并通过邮寄发送到另一个要保存的API?

It looks something like: 看起来像:

  1. User imports a photo through the HTML form and it transforms it to binary BASE64 code: 用户通过HTML表单导入照片,并将其转换为二进制BASE64代码:

     base64_encode = Base64.encode64(img_from_fild_form) 
  2. The data is sent to another API (media): 数据被发送到另一个API(媒体):

     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: 如果输入来自Rails表单,则可能很简单:

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

the uploaded file is likely an instance of ActionDispatch::Http::UploadedFile 上传的文件可能是ActionDispatch::Http::UploadedFile的实例

It should be as easy as: 它应该很简单:

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

Hope that helps. 希望能有所帮助。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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