0

我正在使用 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)

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

4

1 回答 1

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"

于 2012-12-05T20:09:17.133 回答