<%= file_field_tag "uploadfile" %> In ...">

User check file size in rails before loading

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, I can name something like this

 validates_upload_file(params[:uploadfile]) 

Is there any way to check file download before downloading (without using javascript or looking at 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 
+6
source share
3 answers

Here is my code for checking the sizes (I use CarrierWave to download).

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

Greetings.

+13
source

You can use:

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

If I'm not mistaken, all of the above methods check the file size after downloading it (and possibly even processing it), but what happens if I select a 1GB file in the file input field for images, given that javascript is missing or javascript is just disabled? This probablt loads, taking a lot of time, just to say that it is too large, which is simply wrong. I'm new, so I might be wrong about something ...

+2
source

Source: https://habr.com/ru/post/891117/


All Articles