我想执行与 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/