File Upload

Hello all,

Has anyone got some tips on validating files when uploaded?
Validation in the model is a lot cleaner than in the controller and
therefore I was hoping to retain this balance when uploading files.

Has anyone found file upload validation as implemented in the model?

On Dec 18, 6:42 am, “[email protected][email protected]
wrote:

Hello all,

Has anyone got some tips on validating files when uploaded?
Validation in the model is a lot cleaner than in the controller and
therefore I was hoping to retain this balance when uploading files.

Has anyone found file upload validation as implemented in the model?

What kind of validations do you want to do?

Thanks for the response Chovy.
Firstly there is a file uploaded and secondly to check that the file
does not contain zero bites.
I would like to keep everything in the model if possible.

j

On Dec 24, 10:23 pm, Chris O. [email protected]
wrote:

has_attachment call through the :size parameter, ex :size =>
At least these are a couple of ways to do it.

I find that attachment_fu doesn’t give informative error messages,
aside from “Picture is invalid”.

[email protected] wrote:

Thanks for the response Chovy.
Firstly there is a file uploaded and secondly to check that the file
does not contain zero bites.
I would like to keep everything in the model if possible.

j

It depends a little on how you are performing the uploads. If you are
using attachment_fu, as I do, you can set the size range within the
has_attachment call through the :size parameter, ex :size =>
10…1000.bytes. You can also insert a custom validation expression
within one of the validate methods, ex.

def validate
unless self.filename.blank?
errors.add_to_base(“Invalid file size”) unless
(10.kilobytes…10.megabytes).include?(self.size)
end
end

At least these are a couple of ways to do it.

chovy wrote:

On Dec 24, 10:23 pm, Chris O. [email protected]
wrote:

has_attachment call through the :size parameter, ex :size =>
At least these are a couple of ways to do it.

I find that attachment_fu doesn’t give informative error messages,
aside from “Picture is invalid”.

To be honest I too don’t like the way attachment_fu throws all the
validation messages at once, which is why I like to put everything in
the validate method and exclude the validates_as_attachment call. Then
you have total control of what messages get shown to the users, although
I have never seen a generic message like “Picture is invalid before”.