载波将文件编码为base64作为进程 [英] Carrierwave encode file to base64 as process

查看:109
本文介绍了载波将文件编码为base64作为进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决这个问题.我想将文件编码为base64作为该过程之一.

I am trying to solve this. I would like to encode file as base64 as one of the process.

到目前为止,我的代码看起来像

My code so far looks like

class ImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick
  include CarrierWave::MimeTypes

  storage :file

  def extension_white_list
    %w(jpg jpeg png)
  end

  def store_dir
    "/tmp/images/#{model.class.to_s.underscore}/#{model.id}"
  end

  process :set_content_type
  process :format_to_jpg
  process :strip_metadata
  process :encode_base64

  def format_to_jpg
    manipulate! do |img|
      img.format 'jpg'
      img
    end
  end

  def strip_metadata
    manipulate! do |img|
      img.strip
      img
    end
  end

  def encode_base64
    #What should be here?
  end
end

我不确定应该在encode_base64方法中放置什么.编码方法是Base64.encode64(),因为应该将文件内容(可能是self.read)发送给参数.但是我不确定如何遵循Carrierwave的最佳做法.

I am not sure what I should place in the encode_base64 method. The method for encoding is Base64.encode64() as parameter should be sent the file content (self.read probably). but I am not sure how I should do this to follow Carrierwave best practices.

推荐答案

发现我在这里不需要做任何特别的事情.

Find out that I don't have to do anything special here.

def encode_base64
  File.write(path,Base64.encode64(File.read(path)))
end

做魔术

这篇关于载波将文件编码为base64作为进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆