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) %> .... <td><b>Image</b> </td> <td><%= file_field("image", "picture", "size"=>"30") %></td> <td><%= submit_tag(" SUBMIT ") %></td> .... <%= 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 ed@appistry.com <mailto:ed@appistry.com> direct. 314 336 2859 cell. 314 518 0408 http://www.appistry.com <http://www.appistry.com/> -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060815/ff3de144/attachment-0001.html
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 <ed@appistry.com> wrote:> > > > > 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) %> > > ?. > > <td><b>Image</b> </td> > <td><%= file_field("image", "picture", "size"=>"30") %></td> > > <td><%= submit_tag(" SUBMIT ") %></td> > > ?. > > <%= 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 > ed@appistry.com > direct. 314 336 2859 cell. 314 518 0408 > > http://www.appistry.com > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- Derek Haynes 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 Haynes 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 ed@appistry.com direct. 314 336 2859 cell. 314 518 0408 http://www.appistry.com -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Derek Haynes Sent: Tuesday, August 15, 2006 11:31 AM To: rails@lists.rubyonrails.org Subject: Re: [Rails] AGAIN: file object treated as string 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 <ed@appistry.com> wrote:> > > > > 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 Iget 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) %> > > .... > > <td><b>Image</b> </td> > <td><%= file_field("image", "picture", "size"=>"30") %></td> > > <td><%= submit_tag(" SUBMIT ") %></td> > > .... > > <%= 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 theserver> 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, andalso> tried using other examples on the web, it always comes down to thisissue.> Perhaps I am missing something, or this has to do with my ruby versionor> the fact that I''m running this on Apache... > > > > Any help would be greatly appreciated. > > > > > > . . . . . . . . . . . . . . . . . . . > > Eduard V. Kotysh > > Appistry | The Fabric of Business > ed@appistry.com > direct. 314 336 2859 cell. 314 518 0408 > > http://www.appistry.com > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- Derek Haynes 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 _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
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 ed@appistry.com direct. 314 336 2859 cell. 314 518 0408 http://www.appistry.com -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of carmen Sent: Tuesday, August 15, 2006 11:49 AM To: rails@lists.rubyonrails.org Subject: Re: [Rails] AGAIN: file object treated as string On Tue Aug 15, 2006 at 09:30:30AM -0700, Derek Haynes 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_fil ename}") @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 _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
Uploaded files under a certain size are treated as StringIO objects rather than File objects On Tuesday 15 August 2006 07:51, Ed Kotysh wrote:> Derek, > > I have tried using a 7K image and got the same error... > > . . . . . . . . . . . . . . . . . . . > > Eduard V. Kotysh > > Appistry | The Fabric of Business > ed@appistry.com > direct. 314 336 2859 cell. 314 518 0408 > > http://www.appistry.com > > -----Original Message----- > From: rails-bounces@lists.rubyonrails.org > [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Derek Haynes > Sent: Tuesday, August 15, 2006 11:31 AM > To: rails@lists.rubyonrails.org > Subject: Re: [Rails] AGAIN: file object treated as string > > 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 <ed@appistry.com> wrote: > > 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) %> > > > > .... > > > > <td><b>Image</b> </td> > > <td><%= file_field("image", "picture", "size"=>"30") %></td> > > > > <td><%= submit_tag(" SUBMIT ") %></td> > > > > .... > > > > <%= 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 > > ed@appistry.com > > direct. 314 336 2859 cell. 314 518 0408 > > > > http://www.appistry.com > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails
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..
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. -- Posted via http://www.ruby-forum.com/.
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 ed@appistry.com direct. 314 336 2859 cell. 314 518 0408 http://www.appistry.com -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of carmen Sent: Tuesday, August 15, 2006 1:34 PM To: rails@lists.rubyonrails.org Subject: Re: [Rails] AGAIN: file object treated as string 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, nota> 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.. _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails