ruby-on-rails-3 - 将 Base64 图像与 Carrierwave 结合使用

标签 ruby-on-rails-3 base64 carrierwave

我想执行与 base64 photo and paperclip -Rails 类似的操作,但是使用载波。 有人可以解释我在 Carrierwave 中使用 Base64 图像吗?

最佳答案

class ImageUploader < CarrierWave::Uploader::Base

  class FilelessIO < StringIO
    attr_accessor :original_filename
    attr_accessor :content_type
  end

  before :cache, :convert_base64

  def convert_base64(file)
    if file.respond_to?(:original_filename) &&
        file.original_filename.match(/^base64:/)
      fname = file.original_filename.gsub(/^base64:/, '')
      ctype = file.content_type
      decoded = Base64.decode64(file.read)
      file.file.tempfile.close!
      decoded = FilelessIO.new(decoded)
      decoded.original_filename = fname
      decoded.content_type = ctype
      file.__send__ :file=, decoded
    end
    file
  end

关于ruby-on-rails-3 - 将 Base64 图像与 Carrierwave 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7622404/

相关文章:

ruby-on-rails - Heroku 上的 Rails : session variables nil

jquery - 载波;在生产服务器上保存图像并移动它们?

android - 如何使用 Retrofit 2 发送图像数组以及每个图像的数据?

ruby-on-rails - Heroku AWS3 CarrierWave 和 Rails 问题

ruby-on-rails - Rails 不使用生成的 CSS 文件

ruby-on-rails - rails 记录 |自创建以来的时间

ruby-on-rails - 在 Cucumber/Capybara 中使用子域的问题

JavaScript - 将数据库 base64 字符串转换为图像

ios - 通过 PhoneGap 将 PNG 或 JPG 图像保存到 iOS 中的照片

java - 在 Java 和 C# 之间编码和解码 Base64 字符串