我发现Paperclip可以验证文件的内容类型,例如image/jpeg,但是我想特别验证文件扩展名。这是因为我正在使用一个不常见的扩展名,无法得到一致的内容类型。有人知道如何做到这一点,或者有什么好方法吗?
我发现Paperclip可以验证文件的内容类型,例如image/jpeg,但是我想特别验证文件扩展名。这是因为我正在使用一个不常见的扩展名,无法得到一致的内容类型。有人知道如何做到这一点,或者有什么好方法吗?
猜想,没有必要使用paperclip的方法进行验证。您可以使用类似以下的内容:
has_attached_file :attachment
validates_format_of :attachment_file_name, :with => %r{\.(docx|doc|pdf)$}i
编辑:
或者,使用纸夹验证它:
validates_attachment_content_type :attachment, :content_type => 'text/plain'
^它将自动生成内容类型不匹配错误。