# Text file uploads come across as strings instead of files?

**URL:** <https://www.ruby-forum.com/t/text-file-uploads-come-across-as-strings-instead-of-files/83170>\
**Category:** Rails\
**Created:** [January 24, 2007, 1:23am UTC](https://www.ruby-forum.com/t/text-file-uploads-come-across-as-strings-instead-of-files/83170 "2007-01-24T01:23:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [January 24, 2007, 1:23am UTC](https://www.ruby-forum.com/t/text-file-uploads-come-across-as-strings-instead-of-files/83170/1 "2007-01-24T01:23:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [January 24, 2007, 2:04am UTC](https://www.ruby-forum.com/t/text-file-uploads-come-across-as-strings-instead-of-files/83170/2 "2007-01-24T02:04:37Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [January 24, 2007, 2:11am UTC](https://www.ruby-forum.com/t/text-file-uploads-come-across-as-strings-instead-of-files/83170/3 "2007-01-24T02:11:08Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [January 24, 2007, 2:11am UTC](https://www.ruby-forum.com/t/text-file-uploads-come-across-as-strings-instead-of-files/83170/4 "2007-01-24T02:11:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [January 24, 2007, 4:40am UTC](https://www.ruby-forum.com/t/text-file-uploads-come-across-as-strings-instead-of-files/83170/5 "2007-01-24T04:40:20Z")

</div>

On Jan 23, 7:26 pm, “Christopher J. Bottaro” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.

---

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [January 24, 2007, 2:27am UTC](https://www.ruby-forum.com/t/text-file-uploads-come-across-as-strings-instead-of-files/83170/6 "2007-01-24T02:27:09Z")

</div>

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
