我有一个 angularJs 应用程序,它将 base64 编码的图像(或文件)发送到我的 rails4 服务器 api,它使用回形针来存储附件。content_type_validation
在回形针完成之前,一切正常。
由于某种原因,回形针确定内容类型被欺骗并收到以下错误消息:
[paperclip] Content Type Spoof: Filename 1413325092.jpg (["image/jpeg"]), content type discovered from file command: application/octet-stream; charset=binary. See documentation to allow this combination.
我使用以下代码创建回形针附件:
def self.create_from_base64(base64_string)
decoded_data = Base64.decode64(base64_string)
# create 'file' understandable by Paperclip
data = StringIO.new(decoded_data)
data.class_eval do
attr_accessor :content_type, :original_filename
end
# set file properties
data.content_type = 'application/octet-stream'
data.original_filename = "#{Time.now.to_i}.jpg"
end
我尝试了不同的东西,但出于某种原因,即使我设置data.content_type = 'application/octet-stream'
了,错误也是完全相同的,而且回形针被欺骗了。
有任何想法吗?
谢谢,
编辑:
我有以下验证:
validates_attachment_content_type :file, :content_type => [/png\Z/, /jpe?g\Z/, /application\/octet-stream*/]