Christopher J. Bottaro
2007-Jan-24 00:21 UTC
text file uploads come across as strings instead of files ?
Hello, How can I get Rails to treat all uploaded files the same? Text files come across as strings, and other files come across as some kind of file object. Here''s my view code: <% form_for(:content, :url => {:action => :new_content}, :html=>{:multipart => true, :method => "post"}) do |form| %> <%= other fields here %> <%= form.file_field :data, :size => 40 %> <%= submit_tag("Upload File") %> <% end %> Now in my action, I have code like this: file_path = params[:content][:data].local_path() When I upload a text file, then params[:content][:data] is a string instead of a file object. How can I get Rails to treat all uploaded files the same? Thanks for the help. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bill Walton
2007-Jan-24 00:59 UTC
Re: text file uploads come across as strings instead of files ?
Christopher J. Bottaro wrote:> How can I get Rails to treat all uploaded files > the same? Text files come across as strings, > and other files come across as some kind of > file object.Best I know, you can''t. I''m not sure if it''s a Rails thing or not. I think not. Small files (I think < 4K) get delivered as strings. Larger than that and you get a file. Here''s the code I found / stole. Hope it helps. if params[:file_to_upload].instance_of?(Tempfile) FileUtils.copy(params[:file_to_upload].local_path, "#{RAILS_ROOT}/public/#{@filenametouse}") else File.open("#{RAILS_ROOT}/public/#{@filenametouse}","w"){|f| f.write(params[:file_to_upload].read) f.close} I''ll warn you, though, that I''ve just noticed I''ve started having a problem with small files getting corrupted, so there might be something wrong with this code. I''m still investigating and would definitely appreciate it if you''d let me know whether or not it works for you. Best regards, Bill --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Carl Johnson
2007-Jan-24 01:04 UTC
Re: text file uploads come across as strings instead of file
> How can I get Rails to treat all uploaded files the same?I can''t answer this question, but a simple workaround would be to check params[:content][:data].class and act accordingly for String versus File type. You could immediately save a String to a file, for example. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Philip Hallstrom
2007-Jan-24 01:20 UTC
Re: text file uploads come across as strings instead of files ?
> Hello, > > How can I get Rails to treat all uploaded files the same? Text files > come across as strings, and other files come across as some kind of > file object.Don''t know if you can... I don''t think it''s a text file vs data file issue. It''s a file size issue... and I think it''s rooted in the CGI ruby code... As another person said, check it''s class and act accordingly.> > Here''s my view code: > > <% form_for(:content, :url => {:action => :new_content}, > :html=>{:multipart => true, :method => "post"}) do |form| %> > <%= other fields here %> > <%= form.file_field :data, :size => 40 %> > <%= submit_tag("Upload File") %> > <% end %> > > Now in my action, I have code like this: > > file_path = params[:content][:data].local_path() > > When I upload a text file, then params[:content][:data] is a string > instead of a file object. How can I get Rails to treat all uploaded > files the same? > > Thanks for the help. > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Christopher J. Bottaro
2007-Jan-24 01:26 UTC
Re: text file uploads come across as strings instead of files ?
Ahh, thanks for the reply... always makes me feel better that I''m not the only one... ;) The crossover point is around 20k (for me at least). Meaning, files less than 20k come across as StringIO and files greater than 20k come across as File (interestingly different than Tempfile for you). Maybe our differences stem from the fact that I''m using Edge Rails? I am going to use code similar to your snippet... I''ll try to remember to let you know how it works out. Thanks, -- C On Jan 23, 6:59 pm, "Bill Walton" <bill.wal...-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote:> Christopher J. Bottaro wrote: > > How can I get Rails to treat all uploaded files > > the same? Text files come across as strings, > > and other files come across as some kind of > > file object.Best I know, you can''t. I''m not sure if it''s a Rails thing or not. I think > not. Small files (I think < 4K) get delivered as strings. Larger than that > and you get a file. Here''s the code I found / stole. Hope it helps. > > if params[:file_to_upload].instance_of?(Tempfile) > FileUtils.copy(params[:file_to_upload].local_path, > "#{RAILS_ROOT}/public/#{@filenametouse}") > else > File.open("#{RAILS_ROOT}/public/#{@filenametouse}","w"){|f| > f.write(params[:file_to_upload].read) > f.close} > > I''ll warn you, though, that I''ve just noticed I''ve started having a problem > with small files getting corrupted, so there might be something wrong with > this code. I''m still investigating and would definitely appreciate it if > you''d let me know whether or not it works for you. > > Best regards, > Bill--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Christopher J. Bottaro
2007-Jan-24 03:39 UTC
Re: text file uploads come across as strings instead of files ?
On Jan 23, 7:26 pm, "Christopher J. Bottaro" <cjbott...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> and files greater than 20k come > across as File (interestingly different than Tempfile for you).I was mistaken. I don''t want anyone to get bad information from this post, so to clarify, they do come across as instances of Tempfile, not File. My bad, sorry. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---