【问题标题】:Paperclip fails to set attachment_content_type for files without extentions回形针无法为没有扩展名的文件设置附件内容类型
【发布时间】:2023-03-10 10:30:01
【问题描述】:

我正在使用 rails 3 + paperclip + s3(私有存储桶)来允许用户上传文件。

问题是用户可以上传不带扩展名的文件,例如 rails gemfile,它只是“gemfile”而不是“gemfile.txt” 当您上传此文件时,回形针不会设置附件内容类型。

我正在像这样确定 content_type:

self.attachment.instance_write(:content_type, MIME::Types.type_for(self.attachment_file_name).to_s)

有没有更好的方法可以更可靠地解决上述错误?谢谢

【问题讨论】:

  • 你能接受@RachelDRoy 的答案吗?

标签: ruby-on-rails ruby-on-rails-3 amazon-s3 paperclip


【解决方案1】:

您要求 MIME::Types 查找具有给定文件名的类型。它期望在 attachment_file_name 中有文件扩展名(如image_12.jpg:扩展名是.jpg,所以MIME::Types.type_for 知道这个文件是一个图像)。

您在寻找什么:

params[:image].content_type # return the content_type
# assuming :image is the name of the form file field

一点研究:

根据文件返回属于文件的 MIME::Types 列表 文件扩展名.

http://mime-types.rubyforge.org/MIME/Types.html + ctrl-F "type_for"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-19
    • 2014-08-15
    • 2014-02-07
    • 2018-08-05
    • 2014-05-30
    • 2014-06-01
    • 1970-01-01
    相关资源
    最近更新 更多