微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails-3 – 使用带有Carrierwave的base64图像

我想执行类似于 base64 photo and paperclip -Rails的操作,但是使用Carrierwave.
有人能解释我在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

原文地址:https://www.jb51.cc/ruby/264572.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐