如何在 Ruby 中给定 URL 以 base64 编码媒体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1547008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:50:20  来源:igfitidea点击:

How to encode media in base64 given URL in Ruby

ruby-on-railsrubybase64encodeactivesupport

提问by Ramon Tayag

I'm trying to upload an image to PingFM. Their documentationsays:

我正在尝试将图像上传到 PingFM。他们的文档说:

media – base64 encoded media data.

I can access this image via the URL. I tried (practically guessed) this:

我可以通过 URL 访问此图像。我试过(几乎是猜到的)这个:

ActiveSupport::Base64.encode64(open("http://image.com/img.jpg"))

But I get this error:

但我收到此错误:

TypeError: can't convert Tempfile into String
    from /usr/lib/ruby/1.8/base64.rb:97:in `pack'
    from /usr/lib/ruby/1.8/base64.rb:97:in `encode64'
    from (irb):19
    from :0

采纳答案by mtyaka

The openmethod:

open方法:

open("http://image.com/img.jpg")

is returning a Tempfile object, while encode64expects a String.

正在返回一个 Tempfile 对象,而encode64期望一个字符串。

Calling readon the tempfile should do the trick:

调用read临时文件应该可以解决问题:

ActiveSupport::Base64.encode64(open("http://image.com/img.jpg") { |io| io.read })

回答by Marielyn Alvarado

To encode a file:

对文件进行编码:

require 'base64'
Base64.encode64(File.open("file_path", "rb").read)

To produce the file from the encoded string:

从编码字符串生成文件:

require 'base64'
encoded_string = Base64.encode64(File.open("file_path", "rb").read)

File.open(file_name_to_create, "wb") do |file|
    file.write(Base64.decode64(encoded_string))
end

回答by robertodecurnex

This will work too, it's a little cleaner

这也可以,它更干净一点

 require 'base64'

 Base64.encode64(open("file_path").to_a.join)

"How do you decode this back into a file?" - @user94154

“你如何将它解码回文件?” - @用户94154

 require 'base64'

 open('output_file_name.txt', 'w') do |f| 
   f << Base64.decode64( encoded_content )
 end

Where encoded_contentwould be the previously encoded file content return value.

encoded_content先前编码的文件内容返回值在哪里。

回答by khelll

Encode a file to base64 encoding:

将文件编码为 base64 编码:

File.open("output_file","w"){|file| file.write [open("link_to_file").string].pack("m")}

Decode base64 encoded file:

解码 base64 编码文件:

File.open('original', 'wb') {|file| file << (IO.readlines('output_file').to_s.unpack('m')).first }

回答by mld.oscar

Here's my solution:

这是我的解决方案:

1: Put this custom image_tag method into ApplicationHelper, and include ActiveSupport module

1:将此自定义image_tag方法放入ApplicationHelper,并包含ActiveSupport模块

module ApplicationHelper
  include ActiveSupport
  def image_tag_base64(file_path, mime_type = 'image/jpeg', options = {})
    image_tag("data:#{mime_type};base64,#{Base64.encode64(open(file_path) { |io| io.read })}", options)
  end
end

2: Then, inside the view you want to use base64 encoded image use the method like this:

2:然后,在你想要使用 base64 编码图像的视图中使用这样的方法:

<%= image_tag_base64 @model.paperclip_attribute.path(:size), @model.paperclip_attribute.content_type, {class: 'responsive-img etc etc'} %>

3: DONE

3:完成

回答by stevec

In case it's useful to others, here's how to save a screenshot as base64 using Watir

如果对其他人有用,以下是使用以下方法将屏幕截图保存为 base64 的方法 Watir

browser = Watir::Browser.new(:chrome, {:chromeOptions => {:args => ['--headless', '--window-size=1000x1000']}})
browser.goto("http://www.yourimage.com")
browser.screenshot.base64

The beauty of this is you don't need to store the image itself

这样做的好处是你不需要存储图像本身