# Image upload problem

**URL:** <https://www.ruby-forum.com/t/image-upload-problem/93116>\
**Category:** Rails\
**Created:** [April 27, 2007, 7:01pm UTC](https://www.ruby-forum.com/t/image-upload-problem/93116 "2007-04-27T19:01:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jruyle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d2c977/32.png) [@jruyle](https://www.ruby-forum.com/u/jruyle)\
**Post date:** [April 27, 2007, 7:01pm UTC](https://www.ruby-forum.com/t/image-upload-problem/93116/1 "2007-04-27T19:01:40Z")

</div>

I’ve seen posts of a few people who have this problem, but not a single  
response about solving it.

I can’t upload .png files, but .jpg and .gif work fine. When I upload  
just a  
.png, my action gets a StringIO object which breaks when I try to copy  
the  
upload to an absolute location. However, using multiple-field upload, as  
long as one of the to-upload files is _not_ a .png, all of the png files  
will properly upload (TempFile objects now). So:

Upload: test\_image.png --\> Failure (FileUtils evaluating nil.to\_str in  
#cp)

Upload: test\_image1.png, test\_image2.png --\> Also fails

Upload: test\_image1.png, test\_image2.png, test\_image3.jpg --\> Works  
perfectly, I see all three images.

So, what’s going on with Rails and png? Is there a mime type I’m missing  
somewhere? It just doesn’t make any sense.

Thanks

Jason

---

<div class="post-metadata">

**Author:** ![jruyle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d2c977/32.png) [@jruyle](https://www.ruby-forum.com/u/jruyle)\
**Post date:** [April 27, 2007, 9:34pm UTC](https://www.ruby-forum.com/t/image-upload-problem/93116/2 "2007-04-27T21:34:34Z")

</div>

Jason R. wrote:

> I’ve seen posts of a few people who have this problem, but not a single  
> response about solving it.
> 
> I can’t upload .png files, but .jpg and .gif work fine. When I upload  
> just a  
> .png, my action gets a StringIO object which breaks when I try to copy  
> the  
> upload to an absolute location.

Sorry, I can’t picture what you’re doing. What is your “view” and your  
“controller” code ?

Stephan

---

<div class="post-metadata">

**Author:** ![jruyle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d2c977/32.png) [@jruyle](https://www.ruby-forum.com/u/jruyle)\
**Post date:** [April 30, 2007, 4:23pm UTC](https://www.ruby-forum.com/t/image-upload-problem/93116/3 "2007-04-30T16:23:48Z")

</div>

Found out where the problem is, and it’s just coincidence that PNG  
images  
were showing this problem.

I’ve found the problem in but one place:  
[http://blog.vixiom.com/2006/07/26/rails-stringio-file-upload/](http://blog.vixiom.com/2006/07/26/rails-stringio-file-upload/)

In short: file uploads that are less than a certain threshold (15K?)  
become  
StringIO objects, and anything larger is a Tempfile. I thought all would  
be  
Tempfiles.

Then I checked out file\_column (  
[http://opensvn.csie.org/rails\_file\_column/plugins/file\_column/](http://opensvn.csie.org/rails_file_column/plugins/file_column/)) and  
found  
this bit of code:

```
  # stored uploaded file into local_file_path
  # If it was a Tempfile object, the temporary file will be
  # cleaned up automatically, so we do not have to care for this
  if file.respond_to?(:local_path) and file.local_path and

```

File.exists?(file.local\_path)  
FileUtils.copy\_file(file.local\_path, local\_file\_path)  
elsif file.respond\_to?(:read)  
File.open(local\_file\_path, “wb”) { |f| f.write(file.read) }  
else  
raise ArgumentError.new(“Do not know how to handle  
#{file.inspect}”)  
end

So somewhere in Rails (this problem is with both WEBrick and Mongrel, so  
I’m  
assuming it’s not them) there’s logic to turn upload file data into a  
tempfile after a certain size, but I cannot find out where. Would it be  
possible / feasible to make all file uploads Tempfiles or is this not  
possible to know on the server?

Jason

---

<div class="post-metadata">

**Author:** ![jruyle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d2c977/32.png) [@jruyle](https://www.ruby-forum.com/u/jruyle)\
**Post date:** [April 30, 2007, 5:22pm UTC](https://www.ruby-forum.com/t/image-upload-problem/93116/4 "2007-04-30T17:22:10Z")

</div>

> So somewhere in Rails (this problem is with both WEBrick and Mongrel, so  
> I’m  
> assuming it’s not them) there’s logic to turn upload file data into a  
> tempfile after a certain size, but I cannot find out where. Would it be

Look in the ruby file cgi.rb, method “read\_multipart”. This is part of  
Ruby, not Rails.

On my Linux machine it is found in

/usr/lib/ruby/1.8/cgi.rb

Stephan
