加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 23:30:51 所属栏目:百科 来源:网络整理
导读:我想执行类似于 base64 photo and paperclip -Rails的操作,但是使用Carrierwave. 有人能解释我在Carrierwave中使用base64图像吗? 解决方法 class ImageUploader CarrierWave::Uploader::Base class FilelessIO StringIO attr_accessor :original_filename a
我想执行类似于 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读