1

I am using Carrierwave to handle my file uploads and now I would like to add validation so users can't upload files of 1GB or similar.

For now I have done it like this:

class Item < ActiveRecord::Base
    mount_uploader :image, ImageUploader
    validate :file_size

    def file_size
        errors[:image] << "too big" if self.image.parametres[:size] > 100000
    end
end

The problem is that I have a feeling this validation works after file is already processed. After I want to upload big file of 30MB web site is stuck a couple of minutes and then it informs me file is too big. It seems to me file is uploaded in cache and then validation runs. This is not really acceptable.

Is there any other way to run validation? Thank you!

user3304086
  • 901
  • 1
  • 9
  • 19
  • possible duplicate of [javascript file upload size validation](http://stackoverflow.com/questions/3717793/javascript-file-upload-size-validation) – Brad Werth Jul 31 '14 at 21:18

2 Answers2

1

The easiest way would be to write some JS to validate the file size client side before the file transfer takes place.

-1

You can use paperclip gem, it will provide customizable validation for size, type and format.

Ravindra
  • 1,039
  • 2
  • 12
  • 26
  • So does Carrierwave. In any case, it looks like the OP is looking for client-side validation. – Matt Nov 16 '17 at 09:09