validation失败:上传文件的扩展名与其内容不匹配
我正在使用paperclip gem上传文件。 我的回形针gem版是paperclip-4.1.1。 在上传文件时抛出
Validation failed: Upload file has an extension that does not match its contents.
我正在尝试上传xlsx文件。 而且我已经在模型中提到了content_type。
validates_attachment_content_type :upload_file, :content_type => %w(application/msword application/vnd.ms-office application/vnd.ms-excel application/vnd.openxmlformats-officedocument.spreadsheetml.sheet), :message => ', Only XML,EXCEL files are allowed. '
我不知道为什么会发生这种错误。 如果您对此错误有任何疑问,请分享。
摘自日志以显示validation失败:
Command :: file -b --mime-type '/tmp/5249540099071db4e41e119388e9dd6220140513-24023-1jlg4zy' [paperclip] Content Type Spoof: Filename file_for_bulk_upload1.xlsx (["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"]), content type discovered from file command: . See documentation to allow this combination. Command :: file -b --mime-type '/tmp/6f19a4f96154ef7ce65db1d585abdb2820140513-24023-tt4u1e' [paperclip] Content Type Spoof: Filename file_for_bulk_upload1.xlsx (["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"]), content type discovered from file command:
Paperclip欺骗validation检查失败,因为file
命令无法准确确定文件类型。
在content type discovered from file command: .
的日志content type discovered from file command: .
– 期间之前的空白是输出的结果 – 即空白。 但是,比较的另一面纯粹使用了文件扩展名,它正确地被选为excel文件。 因此您的validation失败。
Paperclip的当前版本使用file -b --mime-type
来确定文件,但是所有实现都不支持--mime-type
。 有一个改变使用--mime
而不是它还没有达到一个里程碑。
我想你有一些选择。 你选择哪个取决于你对上传的一些狡猾的文件和被称为excel文件的关注程度。 如果您对此感到担心,请尝试选项1; 如果你不担心选择2或3选项。
1)覆盖欺骗检查以使用--mime
而不是--mime-type
。
覆盖初始化程序中的type_from_file_command
:
module Paperclip class MediaTypeSpoofDetector private def type_from_file_command # -- original code removed -- # begin # Paperclip.run("file", "-b --mime-type :file", :file => @file.path) # rescue Cocaine::CommandLineError # "" # end # -- new code follows -- begin Paperclip.run("file", "-b --mime :file", :file => @file.path) rescue Cocaine::CommandLineError "" end end end end
2)通过从文件扩展名完全设置文件类型来绕过file
检查。
将此Paperclip选项设置为在应用程序初始化期间读取的某个位置(例如config/application.rb
, config/environments/
或config/initializers/paperclip.rb
):
Paperclip.options[:content_type_mappings] = { xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }
3)完全禁用欺骗。
通过在初始化程序中创建类似的内容来覆盖欺骗检查:
module Paperclip class MediaTypeSpoofDetector def spoofed? false end end end
更新:
您在模型中进行的validation不是导致此问题的原因。 这将validation您可以加载哪些类型的文件; 你看到的是Paperclip计算文件的类型是有效但其内容与文件的类型不匹配。
假设您可以使用欺骗validation,您的内容validation会出现一个exception现象。 您输出的错误消息显示“只允许XML,EXCEL文件”,但是您的实际validation是检查MS word和excel文件,而不是xml。
如果您的消息正确并且您确实只想允许xml和excel文件,则应将content_typevalidation更改为:
validates_attachment_content_type :upload_file, :content_type => %w(application/xml application/vnd.ms-excel application/vnd.openxmlformats-officedocument.spreadsheetml.sheet), :message => ', Only XML,EXCEL files are allowed. '
试试这种方式
validates_attachment_content_type :upload_file, :content_type => ["application/msword", "application/vnd.ms-office application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"], :message => ', Only XML,EXCEL files are allowed. '