# File Upload problem when file is small, under 19k(read metho

**URL:** <https://www.ruby-forum.com/t/file-upload-problem-when-file-is-small-under-19k-read-metho/95050>\
**Category:** Rails\
**Created:** [May 20, 2006, 12:02pm UTC](https://www.ruby-forum.com/t/file-upload-problem-when-file-is-small-under-19k-read-metho/95050 "2006-05-20T12:02:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ant\_Peacocke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/779978/32.png) [@Ant\_Peacocke](https://www.ruby-forum.com/u/Ant_Peacocke)\
**Post date:** [May 20, 2006, 12:02pm UTC](https://www.ruby-forum.com/t/file-upload-problem-when-file-is-small-under-19k-read-metho/95050/1 "2006-05-20T12:02:12Z")

</div>

Hi

I hope someone can shed some light on this have searched a lot but found  
nothing definite. This happens on Ubuntu and Fedora 2.

I am having a problem with uploading small files (images gif or jpeg  
under 19k).

According to ruby documentation these are stored temporarily as StringIO  
objects rather than temporary files (as is done with the larger file  
uploads).

The problem is that when it is a StringIO rather than a temporary file  
(ie a file smaller than 19k) nothing is returned from the read method.

example code where “image” is the file upload form parameter, it works  
fine if image is over round 19k but is it is smaller then image.read  
returns nothing however the image.size and image.original\_filename  
return the correct information.

File.open(path\_to\_save\_file, “wb”) {|f| f.write(image.read) }

All the best  
Ant

–  
Ant Peacocke

[www.levelsystems.net](http://www.levelsystems.net)  
T: + 33 (0) 8 70 46 63 37  
M: + 33 (0) 6 09 81 06 00

---

<div class="post-metadata">

**Author:** ![Ant\_Peacocke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/779978/32.png) [@Ant\_Peacocke](https://www.ruby-forum.com/u/Ant_Peacocke)\
**Post date:** [May 20, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/file-upload-problem-when-file-is-small-under-19k-read-metho/95050/2 "2006-05-20T14:18:16Z")

</div>

Ant Peacocke wrote:

> example code where “image” is the file upload form parameter, it works  
> fine if image is over round 19k but is it is smaller then image.read  
> returns nothing however the image.size and image.original\_filename  
> return the correct information.
> 
> File.open(path\_to\_save\_file, “wb”) {|f| f.write(image.read) }

I haven’t seen this myself, so I’m puzzled.

Have you tried doing image.rewind before trying to read the image  
IOString?

–Al Evans

---

<div class="post-metadata">

**Author:** ![Ant\_Peacocke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/779978/32.png) [@Ant\_Peacocke](https://www.ruby-forum.com/u/Ant_Peacocke)\
**Post date:** [May 20, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/file-upload-problem-when-file-is-small-under-19k-read-metho/95050/3 "2006-05-20T18:58:11Z")

</div>

Many thanks Al

that fixed the problem, by putting image.rewind before image.read it now  
reads the file contents regardless of whether they are small or large.  
eg.

image.rewind

File.open(path\_to\_save\_file, “wb”) {|f| f.write(image.read) }

Thanks again Ant

Al Evans wrote:

> I haven’t seen this myself, so I’m puzzled.
> 
> Have you tried doing image.rewind before trying to read the image  
> IOString?
> 
> –Al Evans

–  
Ant Peacocke

[www.levelsystems.net](http://www.levelsystems.net)  
T: + 33 (0) 8 70 46 63 37  
M: + 33 (0) 6 09 81 06 00
