加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Paperclip不支持.doc文件

发布时间:2020-12-17 01:50:37 所属栏目:百科 来源:网络整理
导读:在rails 4.0.2中,我使用paperclip gem上传文件.但它不支持.doc文件.在文件上传字段下方,它显示错误消息“具有与其内容不匹配的扩展名” 在模型中,检查内容类型的验证如下: validates_attachment_content_type :document,:content_type = ['application/txt'
在rails 4.0.2中,我使用paperclip gem上传文件.但它不支持.doc文件.在文件上传字段下方,它显示错误消息“具有与其内容不匹配的扩展名”

在模型中,检查内容类型的验证如下:

validates_attachment_content_type :document,:content_type => ['application/txt','text/plain','application/pdf','application/msword','application/vnd.openxmlformats-officedocument.wordprocessingml.document','application/vnd.oasis.opendocument.text','application/x-vnd.oasis.opendocument.text','application/rtf','application/x-rtf','text/rtf','text/richtext','application/doc','application/docx','application/x-soffice','application/octet-stream']

现在使用的宝石

rails (4.0.2,4.0.0,3.2.13,3.2.8,3.0.4,3.0.3)

paperclip (3.5.2,2.3.11,2.3.8)

我该如何解决这个问题?

解决方法

将此添加到初始化程序以禁用欺骗保护:

require 'paperclip/media_type_spoof_detector'         
module Paperclip         
 class MediaTypeSpoofDetector        
    def spoofed?         
      false       
    end       
  end        
end

对于centOS

module Paperclip     
 class MediaTypeSpoofDetector      
 def type_from_file_command      
  begin       
    Paperclip.run("file","-b --mime :file",:file => @file.path)
  rescue Cocaine::CommandLineError
    ""
  end       
end      
end      
end

从https://github.com/thoughtbot/paperclip/issues/1429

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读