簡體   English   中英

通過Paperclip上傳base64

[英]Uploading base64 through Paperclip

回形針用於圖像上傳。 以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

回形針可以正確識別通過的base64的content_type,但不會進行content_type驗證。 它保存任何文件。

有人可以幫我嗎?

您需要使用before_validation回調而不是before_save

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM