w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How can we restrict file uploads with valid file type but invalid content-type - Rails

I recommend you to check paperclip gem its a file attachment library for Active Record. This library is very straightforward to use and make validations with each content type. For example if you want to validate an Image you can by the next validation:

class User < ActiveRecord::Base
  attr_accessible :avatar
  has_attached_file :avatar, :styles => { :medium => "300x300>",
:thumb => "100x100>" }, :default_url =>
"/images/:style/missing.png"

  validates_attachment :avatar, 
  :presence => true, :content_type => { :content_type =>
"image/jpg" },
  :size => { :in => 0..500.kilobytes }

end

Hope it helps.





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.