Custom validation for file size in rails before being uploaded
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 ...
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, 2022Comments
-
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 helpUPD
validate :uploadfile_validation, :if => "uploadfile?" def uploadfile_validation errors[:uploadfile] << "should be less than 1MB" if uploadfile.size > 1.megabytes end
-
Hishalv almost 13 yearshi, 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 almost 13 yearsTry something like
"!uploadfile.blank?"
instead of"uploadfile?"
- I thinkpicture?
is a method defined by CarrierWave -
Hishalv almost 13 yearsthanks 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 almost 13 yearsDoes this size of the file get checked before or after the file is uploaded?
-
Lucas almost 13 yearsBefore uploading. I just checked right now because I had a doubt but it works fine.
-
Anwar over 8 yearsThis works, but not before upload. File will be uploaded into the cache
-
Rustam A. Gasanov over 8 yearsyep,
client_max_body_size 5M
-
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 over 7 years@MCB correct, check this link nginx.org/en/docs/http/…
-
Filipe Gorges Reuwsaat about 7 yearsI'd stay away from adding validation within a web server configuration file
-
Andrey about 6 yearsRails 5:
..., if: -> { picture }