当文件不是有效的附件内容类型时,Paperclip::NotIdentifiedByImageMagickError

发布于 2024-08-18 21:25:57 字数 1810 浏览 4 评论 0原文

当我尝试上传不在 ["image/jpg"、"image/jpeg"、"image/gif"、"image/png"、"image/pjpeg 中的文件时,出现系统错误", "image/x-png"]

当我尝试上传“wav”等文件时,我收到此消息

* Photo /var/folders/nT/nTr21TWfFRO7r3cyG-h-7+++ “识别”命令无法识别 +TM/-Tmp-/Clip 音频 01,39154,0.wav。 * 照片 /var/folders/nT/nTr21TWfFRO7r3cyG-h-7++++TM/-Tmp-/Clip audio 01,39154,0.wav 无法被“identify”命令识别。 * 照片内容类型 接受的文件包括:jpg、gif、png

因此它检测到该文件不是图像并显示我的消息 “接受的文件包括:jpg、gif、png” 但是在“识别”命令无法识别我的照片之前,我包含了这条额外的消息... 图片上传效果很好

我的代码是:

控制器:

def upload  
  @picture= Picture.new(params[:picture])  
    if [email protected]?  
        render  :form  
    end  
end  

查看表单:

<%= error_messages_for :picture, :header_message => nil, :message => nil %>  
<% form_for :picture, @picture, :name => "uploadPic", :url => { :action => 'upload_data'}, :html => {:name => 'uploadForm', :multipart => true } do |form| %>  
    <%= form.file_field :photo %>  
    <%= submit_tag 'Save'%>  
<% end %>

图片模型:

 class Picture < ActiveRecord::Base    
    require 'paperclip'  
    has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" }  

    validates_attachment_size :photo, :less_than => 2.megabytes , :message => "must be less than 2 megabytes"  
    validates_attachment_content_type :photo, :content_type => ["image/jpg", "image/jpeg", "image/gif", "image/png", "image/pjpeg", "image/x-png"], :message => "Accepted files include: jpg, gif, png"   

 end

I have systematically an error when I'm trying to upload a file that is not in ["image/jpg", "image/jpeg", "image/gif", "image/png", "image/pjpeg", "image/x-png"]

When I try to upload a file like a 'wav' I have this message

* Photo /var/folders/nT/nTr21TWfFRO7r3cyG-h-7++++TM/-Tmp-/Clip audio 01,39154,0.wav is not recognized by the 'identify' command.
* Photo /var/folders/nT/nTr21TWfFRO7r3cyG-h-7++++TM/-Tmp-/Clip audio 01,39154,0.wav is not recognized by the 'identify' command.
* Photo content type Accepted files include: jpg, gif, png

So it detects that the file is not an image and display my message "Accepted files include: jpg, gif, png" but I have this extra message included before mine Photo is not recognized by the 'identify' command...
Upload works fine for pictures

My code is:

Controller:

def upload  
  @picture= Picture.new(params[:picture])  
    if [email protected]?  
        render  :form  
    end  
end  

View form:

<%= error_messages_for :picture, :header_message => nil, :message => nil %>  
<% form_for :picture, @picture, :name => "uploadPic", :url => { :action => 'upload_data'}, :html => {:name => 'uploadForm', :multipart => true } do |form| %>  
    <%= form.file_field :photo %>  
    <%= submit_tag 'Save'%>  
<% end %>

Picture model:

 class Picture < ActiveRecord::Base    
    require 'paperclip'  
    has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" }  

    validates_attachment_size :photo, :less_than => 2.megabytes , :message => "must be less than 2 megabytes"  
    validates_attachment_content_type :photo, :content_type => ["image/jpg", "image/jpeg", "image/gif", "image/png", "image/pjpeg", "image/x-png"], :message => "Accepted files include: jpg, gif, png"   

 end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

铃予 2024-08-25 21:25:57

用 :whiny => 解决了它假
has_attached_file :照片, :whiny => false, :styles => {:中=> "300x300>", :thumb => “100x100>” }

solved it with :whiny => false
has_attached_file :photo, :whiny => false, :styles => { :medium => "300x300>", :thumb => "100x100>" }

赴月观长安 2024-08-25 21:25:57

:发牢骚=> false 不足以解决最新版本的回形针(2.3.6)的问题。我最终在 Rails 初始值设定项中执行此操作:

module Paperclip
  class Attachment
    alias original_assign assign
    def assign(*args)
      original_assign(*args)
    rescue NotIdentifiedByImageMagickError => e
    end
  end
end

似乎可以接受该异常,因为至少如果您使用 :whiny => ,验证错误就会被添加。真的。

:whiny => false was not enough to solve the problem with the latest version of paperclip (2.3.6). I ended up doing this in a rails initializer:

module Paperclip
  class Attachment
    alias original_assign assign
    def assign(*args)
      original_assign(*args)
    rescue NotIdentifiedByImageMagickError => e
    end
  end
end

It seems okay to swallow that exception because validation errors get added anyway at least if you use :whiny => true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文