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.
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.
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.
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
On Jan 23, 7:26 pm, “Christopher J. Bottaro” [email protected]
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.
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