# File Upload

**URL:** <https://www.ruby-forum.com/t/file-upload/123538>\
**Category:** Rails\
**Created:** [December 18, 2007, 3:42pm UTC](https://www.ruby-forum.com/t/file-upload/123538 "2007-12-18T15:42:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 18, 2007, 3:42pm UTC](https://www.ruby-forum.com/t/file-upload/123538/1 "2007-12-18T15:42:44Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 18, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/file-upload/123538/2 "2007-12-18T19:30:59Z")

</div>

On Dec 18, 6:42 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 19, 2007, 12:20pm UTC](https://www.ruby-forum.com/t/file-upload/123538/3 "2007-12-19T12:20:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 25, 2007, 1:16pm UTC](https://www.ruby-forum.com/t/file-upload/123538/4 "2007-12-25T13:16:28Z")

</div>

On Dec 24, 10:23 pm, Chris O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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”.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 25, 2007, 7:23am UTC](https://www.ruby-forum.com/t/file-upload/123538/5 "2007-12-25T07:23:15Z")

</div>

removed\_email\_address@domain.invalid 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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 25, 2007, 4:41pm UTC](https://www.ruby-forum.com/t/file-upload/123538/6 "2007-12-25T16:41:50Z")

</div>

chovy wrote:

> On Dec 24, 10:23 pm, Chris O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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”.
