【问题标题】:How do I upload a csv file on Windows using Paperclip paperclip 4.3.2如何使用 Paperclip 回形针 4.3.2 在 Windows 上上传 csv 文件
【发布时间】:2016-03-15 01:09:09
【问题描述】:

Paperclip 有一个众所周知的问题。

https://github.com/thoughtbot/paperclip/issues/1924

我如何配置我的模型,以使这种愚蠢的欺骗验证起作用?

在发现问题之前我使用的是:

validates_attachment_content_type :csv_import, :content_type => 'text/csv'

但这不适用于某些版本的 Windows。在 Windows 7 Professional 上,我收到此错误:

[paperclip] Content Type Spoof: Filename delivery_detail.csv (application/octet-stream from Headers, [#<MIME::Type:0x00000005077f38 @content_type="text/csv", @raw_media_type="text", @raw_sub_type="csv", @simplified="text/csv", @media_type="text", @sub_type="csv", @extensions=["csv"], @encoding="8bit", @system=nil, @registered=true, @url=["IANA", "RFC4180"], @obsolete=nil, @docs=nil>, #<MIME::Type:0x000000050c7f60 @content_type="text/comma-separated-values", @raw_media_type="text", @raw_sub_type="comma-separated-values", @simplified="text/comma-separated-values", @media_type="text", @sub_type="comma-separated-values", @extensions=["csv"], @encoding="8bit", @system=nil, @registered=false, @url=nil, @obsolete="!", @docs="use-instead:text/csv", @use_instead=["text/csv"]>] from Extension), content type discovered from file command: text/plain. See documentation to allow this combination.

有没有人成功制作回形针上传 csv 文件?

我从 Github 问题报告中尝试了所有可能的解决方法,但没有任何效果。我需要查看有效的示例解决方案。

更新 1

sonianand11 于 2014 年 10 月 2 日发表评论

https://github.com/thoughtbot/paperclip/issues/1470

这可行,但它涉及关闭内容验证,有更好的方法吗?

【问题讨论】:

    标签: ruby-on-rails paperclip spoofing


    【解决方案1】:

    我想出了以下解决方案:

    添加到模型中:

    validates_attachment_content_type :my_csv_uploaded_file, content_type: ['text/plain', 'text/csv', 'application/vnd.ms-excel']
    

    到初始化器:

    Paperclip.options[:content_type_mappings] = { csv: 'application/vnd.ms-excel' }
    

    它对我有用。已使用 Windows 7 Professional 进行测试

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-14
      • 2014-05-23
      • 2012-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多