Ruby on rails 通过回形针上传base64

Ruby on rails 通过回形针上传base64,ruby-on-rails,base64,paperclip-validation,Ruby On Rails,Base64,Paperclip Validation,回形针用于图像上传。以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)

回形针用于图像上传。以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
class-Photo%w(image/jpeg-image/jpg-image/png)
def set_图像
除非self.picture\u data.nil?
data=StringIO.new(Base64.decode64(self.picture\u数据))
self.image=数据
self.picture_data=nil
结束
结束
结束
回形针正确识别传递的base64的内容类型,但未进行内容类型验证。它保存任何文件


谁能帮我度过这段时间

您需要在验证之前使用
回调,而不是在保存之前使用

实际上,我对“has_attached_file:image”和“self.avatar=data”这两个字段感到困惑,这两个字段实际用于存储图像?对不起,搅拌机。已更正。请参阅此链接,可能会对您有所帮助。