Paperclip- validate pdfs with content_type='application/octet-stream'

前端 未结 3 1058
走了就别回头了
走了就别回头了 2020-12-28 09:58

I was using paperclip for file upload. with validations as below:

validates_attachment_content_type :upload, :content_type=>[\'application/pdf\

3条回答
  •  清酒与你
    2020-12-28 10:52

    For paperclip 3.3 and Rails 3, I did this a bit differently

    before_validation on: :create do   
      if media_content_type == 'application/octet-stream'
        mime_type = MIME::Types.type_for(media_file_name) 
        self.media_content_type = mime_type.first if mime_type.first  
      end
    end
    
    validates_attachment :media, content_type: { content_type: VALID_CONTENT_TYPES } 
    

    By the way, i needed to do this because testing with Capybara and phantom js using attach_file did not generate the correct mime type for some files.

提交回复
热议问题