# AGAIN: file object treated as string

**URL:** <https://www.ruby-forum.com/t/again-file-object-treated-as-string/65013>\
**Category:** Rails\
**Created:** [August 15, 2006, 6:05pm UTC](https://www.ruby-forum.com/t/again-file-object-treated-as-string/65013 "2006-08-15T18:05:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ed\_Kotysh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/d6d6ee/32.png) [@Ed\_Kotysh](https://www.ruby-forum.com/u/Ed_Kotysh)\
**Post date:** [August 15, 2006, 6:05pm UTC](https://www.ruby-forum.com/t/again-file-object-treated-as-string/65013/1 "2006-08-15T18:05:58Z")

</div>

I am trying to implement the uploading of a file to a remote server

I get error while trying to write the file on the server. The error I  
get is the following:

undefined method `rewind’ for #String:0x2aaaad062eb8

It seems to be treating my file as a string instead of a File object.

--------- Code is below ------------------

VIEW:

\<%= start\_form\_tag(:action =\> “save\_image”, :multipart =\> true) %\>

…

**Image** \<%= file\_field("image", "picture", "size"=\>"30") %\> \<%= submit\_tag(" SUBMIT ") %\>

…

\<%= end\_form\_tag %\>

CONTROLLER:

```
    def save_image
            @image = Image.new(params[:image])

            # attempt to save entry in the db
            if @image.save
                    # info saved in db OK. Save file on the server
                    write_file(params[:image])
                    redirect_to(:controller => 'shto', :action =>

```

‘index’)  
else  
render(:action =\> :get)  
end  
end

```
    def write_file(uploaded_file)
                    uploaded_file['picture'].rewind

```

File.open(“pictures/upload/#{@image.id}.#{@image.extention}”, “wb”) {  
|f| f.write(uploaded\_file[‘picture’].read) }  
end  
end

MODEL:

class Image \< ActiveRecord::Base

```
    def picture=(image_field)
            extname_dot = File.extname(image_field)
            self.extention = extname_dot.slice(1,

```

extname\_dot.length)  
self.submitDate = Time.now  
end

end

I tried implementing this with Agile web development rails book, and  
also tried using other examples on the web, it always comes down to this  
issue. Perhaps I am missing something, or this has to do with my ruby  
version or the fact that I’m running this on Apache…

Any help would be greatly appreciated.

. . . . . . . . . . . . . . . . . . .

Eduard V. Kotysh

Appistry | The Fabric of Business  
removed\_email\_address@domain.invalid [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
direct. 314 336 2859 cell. 314 518 0408

[http://www.appistry.com](http://www.appistry.com) [http://www.appistry.com/](http://www.appistry.com/)

---

<div class="post-metadata">

**Author:** ![Ed\_Kotysh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/d6d6ee/32.png) [@Ed\_Kotysh](https://www.ruby-forum.com/u/Ed_Kotysh)\
**Post date:** [August 15, 2006, 6:33pm UTC](https://www.ruby-forum.com/t/again-file-object-treated-as-string/65013/2 "2006-08-15T18:33:03Z")

</div>

Eduard,

What is the size of the file you are testing with? I believe there is  
a minimum size for a TempFile, something like 13K.

- Derek

On 8/15/06, Ed Kotysh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> --------- Code is below ------------------
> 
> ```
> redirect_to(:controller => 'shto', :action =>
> 
> ```
> 
> Perhaps I am missing something, or this has to do with my ruby version or  
> . . . . . . . . . . . . . . . . . . .
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Derek H.  
Highgroove Studios - [http://www.highgroove.com](http://www.highgroove.com)  
San Mateo, CA | Atlanta, GA  
Keeping it Simple.  
650.276.0908

Slingshot - Ruby on Rails Business Hosting  
[http://www.slingshothosting.com](http://www.slingshothosting.com)

---

<div class="post-metadata">

**Author:** ![Ed\_Kotysh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/d6d6ee/32.png) [@Ed\_Kotysh](https://www.ruby-forum.com/u/Ed_Kotysh)\
**Post date:** [August 15, 2006, 6:51pm UTC](https://www.ruby-forum.com/t/again-file-object-treated-as-string/65013/3 "2006-08-15T18:51:06Z")

</div>

On Tue Aug 15, 2006 at 09:30:30AM -0700, Derek H. wrote:

> Eduard,
> 
> What is the size of the file you are testing with? I believe there is  
> a minimum size for a TempFile, something like 13K.

yeah, you need to branch your upload code a bit to handle whether its a  
file on disk, or in a string in memory:

```
i=p[:imagefile]
if i.respond_to? "local_path" and i.local_path and 

```

File.exists?(i.local\_path)  
subdir=i.original\_filename.split("")[0…1].join("/")  
FileUtils.mkdir\_p “/app/public/images/#{subdir}”  
FileUtils.mv(i.local\_path,"/app/public/images/#{subdir}/#{i.original\_filename}")  
@e.link(Image.new({:name =\> i.full\_original\_filename,:src=\> subdir

- “/” + i.original\_filename}),“image”)  
elsif i.respond\_to?(:read)  
subdir=i.original\_filename.split("")[0…1].join("/")  
FileUtils.mkdir\_p “/app/public/images/#{subdir}”  
File.open("/app/public/images/#{subdir}/#{i.original\_filename}",  
“wb”) { |f| f.write(i.read) }  
@e.link(Image.new({:name =\> i.full\_original\_filename,:src=\> subdir
- “/” + i.original\_filename}),“image”)  
end

cheers  
c

---

<div class="post-metadata">

**Author:** ![Ed\_Kotysh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/d6d6ee/32.png) [@Ed\_Kotysh](https://www.ruby-forum.com/u/Ed_Kotysh)\
**Post date:** [August 15, 2006, 7:54pm UTC](https://www.ruby-forum.com/t/again-file-object-treated-as-string/65013/4 "2006-08-15T19:54:15Z")

</div>

Derek,

I have tried using a 7K image and got the same error…

. . . . . . . . . . . . . . . . . . .

Eduard V. Kotysh

Appistry | The Fabric of Business  
removed\_email\_address@domain.invalid  
direct. 314 336 2859 cell. 314 518 0408

[http://www.appistry.com](http://www.appistry.com)

---

<div class="post-metadata">

**Author:** ![Ed\_Kotysh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/d6d6ee/32.png) [@Ed\_Kotysh](https://www.ruby-forum.com/u/Ed_Kotysh)\
**Post date:** [August 15, 2006, 8:06pm UTC](https://www.ruby-forum.com/t/again-file-object-treated-as-string/65013/5 "2006-08-15T20:06:17Z")

</div>

I’m not sure I understand why it would be a string in memory…  
When you do i=p[:imagefile] “i[‘picture’]” should give me a file, not a  
string because I did a \<%= file\_field(“image”, “picture”, “size”=\>“30”)  
%\> in my form. No?

. . . . . . . . . . . . . . . . . . .

Eduard V. Kotysh

Appistry | The Fabric of Business  
removed\_email\_address@domain.invalid  
direct. 314 336 2859 cell. 314 518 0408

[http://www.appistry.com](http://www.appistry.com)

---

<div class="post-metadata">

**Author:** ![Ed\_Kotysh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/d6d6ee/32.png) [@Ed\_Kotysh](https://www.ruby-forum.com/u/Ed_Kotysh)\
**Post date:** [August 15, 2006, 8:36pm UTC](https://www.ruby-forum.com/t/again-file-object-treated-as-string/65013/6 "2006-08-15T20:36:24Z")

</div>

Uploaded files under a certain size are treated as StringIO objects  
rather  
than File objects

---

<div class="post-metadata">

**Author:** ![Ed\_Kotysh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/d6d6ee/32.png) [@Ed\_Kotysh](https://www.ruby-forum.com/u/Ed_Kotysh)\
**Post date:** [August 15, 2006, 8:36pm UTC](https://www.ruby-forum.com/t/again-file-object-treated-as-string/65013/7 "2006-08-15T20:36:26Z")

</div>

On Tue Aug 15, 2006 at 01:04:44PM -0500, Ed Kotysh wrote:

> I’m not sure I understand why it would be a string in memory…  
> When you do i=p[:imagefile] “i[‘picture’]” should give me a file, not a  
> string because I did a \<%= file\_field(“image”, “picture”, “size”=\>“30”)  
> %\> in my form. No?

it is a file, its either binary data stored in a String object, or a  
TempFile on disk. i had the same problem, and found the solution buried  
inside one of the file plugins out there. the cutoff size could even  
vary on architectures with different word sizes; i have a feeling its  
32K at most.

it is slightly inconsistent, since something somewhere (CGI module  
perhaps?) is making an executive decision as to how to cache the  
uploaded file…

---

<div class="post-metadata">

**Author:** ![Ed\_Kotysh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/d6d6ee/32.png) [@Ed\_Kotysh](https://www.ruby-forum.com/u/Ed_Kotysh)\
**Post date:** [August 15, 2006, 10:06pm UTC](https://www.ruby-forum.com/t/again-file-object-treated-as-string/65013/8 "2006-08-15T22:06:44Z")

</div>

I have tried uploading small and large images (sizes varied from 7k to  
3mb) and the same problem occurs. I tried looking at the cgi.rb file  
which lived in activesupport-1.3.1/lib/active\_support/core\_ext/ but this  
was all it contained:

require File.dirname( **FILE** ) + ‘/cgi/escape\_skipping\_slashes’

class CGI #:nodoc:  
extend(ActiveSupport::CoreExtensions::CGI::EscapeSkippingSlashes)  
end

Could something else be making this decision about how to store the  
file?  
Thank you

. . . . . . . . . . . . . . . . . . .

Eduard V. Kotysh

Appistry | The Fabric of Business  
removed\_email\_address@domain.invalid  
direct. 314 336 2859 cell. 314 518 0408

[http://www.appistry.com](http://www.appistry.com)

---

<div class="post-metadata">

**Author:** ![Ed\_Kotysh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/d6d6ee/32.png) [@Ed\_Kotysh](https://www.ruby-forum.com/u/Ed_Kotysh)\
**Post date:** [August 15, 2006, 8:48pm UTC](https://www.ruby-forum.com/t/again-file-object-treated-as-string/65013/9 "2006-08-15T20:48:19Z")

</div>

carmen wrote:

> On Tue Aug 15, 2006 at 01:04:44PM -0500, Ed Kotysh wrote:
> 
> > I’m not sure I understand why it would be a string in memory…  
> > When you do i=p[:imagefile] “i[‘picture’]” should give me a file, not a  
> > string because I did a \<%= file\_field(“image”, “picture”, “size”=\>“30”)  
> > %\> in my form. No?
> 
> it is a file, its either binary data stored in a String object, or a  
> TempFile on disk. i had the same problem, and found the solution buried  
> inside one of the file plugins out there. the cutoff size could even  
> vary on architectures with different word sizes; i have a feeling its  
> 32K at most.
> 
> it is slightly inconsistent, since something somewhere (CGI module  
> perhaps?) is making an executive decision as to how to cache the  
> uploaded file…

I had a lot of issues trying to get the StringIO object to function  
properly. I finally just opened up the cgi.rb source file and found the  
if statement that branches between creating a TempFile object and a  
StringIO object. I changed the if statement so that basically everything  
gets turned into a TempFile object.
