2012-12-05 42 views
0

我正在使用rails 3 + paperclip + s3(专用存储桶)来允许用户上传文件。回形针无法为无扩展名的文件设置attachment_content_type

用户可以上传没有扩展名的文件,例如rails gemfile,它只是“gemfile”而不是“gemfile.txt”当您上传此文件时,paperclip不会设置attachment_content_type。

我确定CONTENT_TYPE像这样:

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

有没有更好的办法做到这一点,更可靠,解决了上述的错误?谢谢

+0

你能接受的答案@RachelDRoy? – MrYoshiji

回答

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 ::类型列表分机号码

http://mime-types.rubyforge.org/MIME/Types.html + Ctrl + F “type_for”

相关问题