我使用 Rails 3 和 paperclip 。我的逻辑允许用户上传图像。除非用户选择的文件不是图像,否则这样可以正常工作。
例如,如果用户选择了文本文件,则验证通过但最终会出现此错误:
5 errors prohibited the profile update:
Profile pic content type is not one of image/jpeg, image/png, image/gif
Profile pic /var/folders/lF/lF0Ne5vGFj44kV54W3zBdU+++TI/-Tmp-/stream20101118-229-17xuiu4-0.js is not recognized by the 'identify' command.
Profile pic /var/folders/lF/lF0Ne5vGFj44kV54W3zBdU+++TI/-Tmp-/stream20101118-229-17xuiu4-0.js is not recognized by the 'identify' command.
Profile pic /var/folders/lF/lF0Ne5vGFj44kV54W3zBdU+++TI/-Tmp-/stream20101118-229-17xuiu4-0.js is not recognized by the 'identify' command.
Profile pic /var/folders/lF/lF0Ne5vGFj44kV54W3zBdU+++TI/-Tmp-/stream20101118-229-17xuiu4-0.js is not recognized by the 'identify' command.
至少第一个错误是指文件类型。但是如果用户上传了一些更具体的文件,比如.PXM
,那么Rails表现得很奇怪并显示出来:
4 errors prohibited the profile update:
Profile pic /var/folders/lF/lF0Ne5vGFj44kV54W3zBdU+++TI/-Tmp-/stream20101118-229-1scwkg7-0.pxm is not recognized by the 'identify' command.
Profile pic /var/folders/lF/lF0Ne5vGFj44kV54W3zBdU+++TI/-Tmp-/stream20101118-229-1scwkg7-0.pxm is not recognized by the 'identify' command.
Profile pic /var/folders/lF/lF0Ne5vGFj44kV54W3zBdU+++TI/-Tmp-/stream20101118-229-1scwkg7-0.pxm is not recognized by the 'identify' command.
Profile pic /var/folders/lF/lF0Ne5vGFj44kV54W3zBdU+++TI/-Tmp-/stream20101118-229-1scwkg7-0.pxm is not recognized by the 'identify' command.
有谁知道这里发生了什么?我的模型中有以下代码:
validates_attachment_content_type :profile_pic, :content_type=>['image/jpeg', 'image/png', 'image/gif']
...和这篇论文剪辑初始化程序:
Paperclip.options[:command_path] = "/opt/local/bin/"
ImageMagik似乎已正确安装和设置:
$ which Magick-config
/opt/local/bin/Magick-config
谢谢!
答案 0 :(得分:3)
我在Paperclip和Rails 2.3.8中遇到了同样的问题。
在模型的has_attached_file
声明中,删除任何非图像文件的:styles
。
答案 1 :(得分:3)
只需将下面的代码放在模型上即可。它不会处理非图像文件。
before_post_process :image?
def image?
!(data_content_type =~ /^image.*/).nil?
end