0

回形针用于图像上传。以base64格式上传的图片如下:

class Photo < ActiveRecord::Base

  before_save :set_image
  attr_accessor :picture_data

  has_attached_file :image
  validates_attachment_content_type :image, :content_type => %w(image/jpeg image/jpg image/png)

  def set_image
    unless self.picture_data.nil?
      data = StringIO.new(Base64.decode64(self.picture_data))
      self.image = data
      self.picture_data = nil
    end
  end

end

Paperclip 正确识别通过的 base64 的 content_type,但没有进行 content_type 验证。它保存任何文件。

谁能帮我解决这个问题?!

4

1 回答 1

0

您需要使用before_validation回调而不是before_save.

于 2017-02-06T12:47:11.493 回答