Custom validation for file size in rails before being uploaded

13,551

Solution 1

Here's my code for size validations (I use CarrierWave for uploads).

  validate :picture_size_validation, :if => "picture?"  

  def picture_size_validation
    errors[:picture] << "should be less than 1MB" if picture.size > 1.megabytes
  end

Cheers.

Solution 2

You can use:

validates_size_of :picture, maximum: 1.megabytes, message: "should be less than 1MB"

Solution 3

If I am not mistaken, then all those methods above check the file size once its been uploaded ( and maybe even processed ) but what happens if I choose a 1GB file in file input field for pictures considering that javascript validation is absent or javascript is just disabled? It probablt gets uploaded, taking a lot of time just to tell ya that its too big, that just aint right. I am a newbie so I might be wrong about something ...

Share:
13,551
Hishalv
Author by

Hishalv

I am mechanical engineer by profession, programming is a hobby, love rails and ruby. Enjoy working with robotics and solving complex problems for energy related systems.

Updated on June 24, 2022

Comments

  • Hishalv
    Hishalv almost 2 years

    in my form i have

    <%= label_tag("file", "Attachment:") %><%= file_field_tag "uploadfile" %>
    

    In my model i would like to write this

    validate :validates_uploadfile
    
    def validates_uploadfile(file)
        max_size = 2048
        errors.add(:uploadfile, "File size exceeds limitation") if file.size > max_size
    end
    

    In my controller can i call something like this

    validates_upload_file(params[:uploadfile])
    

    Is there a way to validate a file upload before being uploaded(not by using javascript or by looking at the file extension)
    Thanks for the help

    UPD

    validate :uploadfile_validation, :if => "uploadfile?"
    
    def uploadfile_validation
        errors[:uploadfile] << "should be less than 1MB" if uploadfile.size > 1.megabytes
    end
    
  • Hishalv
    Hishalv almost 13 years
    hi, i have edited my code above, however i get an error undefined local variable or method `uploadfile', any thoughts. iam not using carrierwave or paperclip, i am uploading directly to s3.
  • Lucas
    Lucas almost 13 years
    Try something like "!uploadfile.blank?" instead of "uploadfile?" - I think picture? is a method defined by CarrierWave
  • Hishalv
    Hishalv almost 13 years
    thanks for the help, i finally installed carrierwave, as things were not working out, after some time i got the above code snippet to work, thanks again.
  • toy
    toy almost 13 years
    Does this size of the file get checked before or after the file is uploaded?
  • Lucas
    Lucas almost 13 years
    Before uploading. I just checked right now because I had a doubt but it works fine.
  • Anwar
    Anwar over 8 years
    This works, but not before upload. File will be uploaded into the cache
  • Rustam A. Gasanov
    Rustam A. Gasanov over 8 years
    yep, client_max_body_size 5M
  • MCB
    MCB over 7 years
    @RustamA.Gasanov So, just to make sure. If I write absolutely no code any files larger than 5 MB wont upload?
  • Rustam A. Gasanov
    Rustam A. Gasanov over 7 years
    @MCB correct, check this link nginx.org/en/docs/http/…
  • Filipe Gorges Reuwsaat
    Filipe Gorges Reuwsaat about 7 years
    I'd stay away from adding validation within a web server configuration file
  • Andrey
    Andrey about 6 years
    Rails 5: ..., if: -> { picture }