AGAIN: file object treated as string

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
[email protected] mailto:[email protected]
direct. 314 336 2859 cell. 314 518 0408

http://www.appistry.com http://www.appistry.com/

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 [email protected] 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
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Derek H.
Highgroove Studios - 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

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

Derek,

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

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

Eduard V. Kotysh

Appistry | The Fabric of Business
[email protected]
direct. 314 336 2859 cell. 314 518 0408

http://www.appistry.com

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
[email protected]
direct. 314 336 2859 cell. 314 518 0408

http://www.appistry.com

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

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 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
[email protected]
direct. 314 336 2859 cell. 314 518 0408

http://www.appistry.com

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs