# File.size() on Uploaded Images Fail

**URL:** <https://www.ruby-forum.com/t/file-size-on-uploaded-images-fail/95348>\
**Category:** Rails\
**Created:** [May 29, 2006, 11:21pm UTC](https://www.ruby-forum.com/t/file-size-on-uploaded-images-fail/95348 "2006-05-29T23:21:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwigal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jwigal](https://www.ruby-forum.com/u/jwigal)\
**Post date:** [May 29, 2006, 11:21pm UTC](https://www.ruby-forum.com/t/file-size-on-uploaded-images-fail/95348/1 "2006-05-29T23:21:12Z")

</div>

I am checking the file size of an uploaded file and storing it in a  
filesize  
column. It will work just fine when the user uploads any files other  
than  
images (jpg,png,gif).

Word, Excel, Powerpoint, .zip and more all work fine.

This is my model asset.rb that handles the file upload.

def newfile=(newfile\_field)  
self.filename = base\_part\_of(newfile\_field.original\_filename)  
self.filetype = newfile\_field.content\_type.chomp  
self.filesize = File.size(newfile\_field) ### It fails on this line  
for  
images…  
end

The error I get is: “can’t convert StringIO into String”

I’m sure there’s an easier method/property to get the file size of the  
uploaded file. Like, reading the HTTP headers?

How do they get the file size in the validator?  
validates\_filesize\_of :field, :in =\> 15.kilobytes…1.megabyte

Thanks for any leads,

Jeff

---

<div class="post-metadata">

**Author:** ![jwigal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jwigal](https://www.ruby-forum.com/u/jwigal)\
**Post date:** [May 29, 2006, 11:37pm UTC](https://www.ruby-forum.com/t/file-size-on-uploaded-images-fail/95348/2 "2006-05-29T23:37:13Z")

</div>

An update…

If I move that File.size() logic out of the model and into the  
controller I  
can get it to work.

Just after where I am saving the file, I quickly check the file size,  
set  
the model’s attribute and re-save it. Kind of messy!

def upload  
@asset = Asset.new(params[:asset])  
if @asset.save  
File.open("#{RAILS\_ROOT}/public/files/#{@asset.filename\_as\_id}",  
“wb”)  
do |f|  
f.write(params[:asset][:newfile].read)  
end  
@asset.filesize = File.size("#{  
RAILS\_ROOT}/public/files/#{@asset.filename\_as\_id}")  
@asset.save  
redirect\_to(:back)  
else  
render(:action =\> :get)  
end  
end

---

<div class="post-metadata">

**Author:** ![jwigal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jwigal](https://www.ruby-forum.com/u/jwigal)\
**Post date:** [May 29, 2006, 11:51pm UTC](https://www.ruby-forum.com/t/file-size-on-uploaded-images-fail/95348/3 "2006-05-29T23:51:16Z")

</div>

> def newfile=(newfile\_field)  
> self.filename =  
> base\_part\_of(newfile\_field.original\_filename)  
> self.filetype = newfile\_field.content\_type.chomp  
> self.filesize = File.size(newfile\_field) ### It fails on this line for  
> images…  
> end
> 
> The error I get is: “can’t convert StringIO into String”

File.size tells you the size of the file you give it by name, eg  
File.size(’/etc/passwd/’). You have a StringIO object, which is just  
another type of IO object. You want to use newfile\_field.size.

---

<div class="post-metadata">

**Author:** ![jwigal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jwigal](https://www.ruby-forum.com/u/jwigal)\
**Post date:** [May 30, 2006, 12:03am UTC](https://www.ruby-forum.com/t/file-size-on-uploaded-images-fail/95348/4 "2006-05-30T00:03:57Z")

</div>

Of course! This works.

Or course this works.

🙂

Thanks,

Jeff
