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

ruby-on-rails-3 – 如何验证文件内容类型为pdf,word,excel和纯

发布时间:2020-12-16 20:05:18 所属栏目:百科 来源:网络整理
导读:在我的模型中: has_attached_file :uploaded_file,:url = "/policy_documents/get/:id",:path = "/public/policy_documents/:id/:basename.:extension" validates_attachment_size :uploaded_file,:less_than = 10.megabytes validates_attachment_presence
在我的模型中:
has_attached_file :uploaded_file,:url => "/policy_documents/get/:id",:path => "/public/policy_documents/:id/:basename.:extension" 

    validates_attachment_size :uploaded_file,:less_than => 10.megabytes    
    validates_attachment_presence :uploaded_file 
     validates_attachment_content_type :uploaded_file,:content_type =>['application/pdf','application/xlsx'],:message => ',Only PDF,EXCEL,WORD or TEXT files are allowed. '

此后,它只能上传PDF文档,而不是excel或word或文本文档.请帮我我失踪的地方

解决方法

我不知道你是否为自己解决了这个问题,但是你想要处理的文档缺少MIME类型,请尝试更改:content_type:
:content_type => ["application/pdf","application/vnd.ms-excel","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet","application/msword","application/vnd.openxmlformats-officedocument.wordprocessingml.document","text/plain"]

或使用自定义验证

validate :correct_content_type,:message => ",WORD or TEXT files are allowed."


def correct_content_type 
  acceptable_types = ["application/pdf","text/plain"]
  acceptable_types.include? uploaded_file.content_type.chomp
end

(编辑:李大同)

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

    推荐文章
      热点阅读