# Simple File Upload is Failing - String vs File

**URL:** <https://www.ruby-forum.com/t/simple-file-upload-is-failing-string-vs-file/103408>\
**Category:** Rails\
**Created:** [May 5, 2007, 3:57pm UTC](https://www.ruby-forum.com/t/simple-file-upload-is-failing-string-vs-file/103408 "2007-05-05T15:57:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac8455/32.png) [@vanik](https://www.ruby-forum.com/u/vanik)\
**Post date:** [May 5, 2007, 3:57pm UTC](https://www.ruby-forum.com/t/simple-file-upload-is-failing-string-vs-file/103408/1 "2007-05-05T15:57:53Z")

</div>

Hi Everyone,

```
Every time I think I'm "getting" RoR something trivial kicks my

```

butt. All I am trying to do is upload text files for processing.  
I’ve stripped everything out of my code in order to troubleshoot this  
and I’ve hit a wall ☹ :

Form:  
\<%= form\_tag :action=\>“savefile”, :multipart=\>true%\>  
\<%= file\_field (:scanfile, :thefile) %\>  
\<%= submit\_tag “upload file” %\>  
\<%= end\_form\_tag %\>

controller:

def savefile  
@thefile = params[:scanfile][:thefile]  
render\_text @thefile.content\_type  
end

The problem is that what I get from the params hash is always just a  
plain string. It is not a file object with .read or .content\_type  
methods. I get this error in firefox:  
undefined method `conent\_type’ for “scandata.txt”:String

Can anyone spot what I have done wrong? I swear that I’ve used this  
same simple approach successfully in other apps.

Thanks,

Gary

---

<div class="post-metadata">

**Author:** ![vanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac8455/32.png) [@vanik](https://www.ruby-forum.com/u/vanik)\
**Post date:** [May 5, 2007, 4:44pm UTC](https://www.ruby-forum.com/t/simple-file-upload-is-failing-string-vs-file/103408/2 "2007-05-05T16:44:56Z")

</div>

Hi Gary,

Gary H. wrote:

> The problem is that what I get from the params hash is  
> always just a plain string. It is not a file object with  
> .read or .content\_type methods. I get this error in firefox:  
> undefined method `conent\_type’ for “scandata.txt”:String

There’s a real good explanation of the what \ why \ how-to for the  
problem  
you’re having at  
[http://cleanair.highgroove.com/articles/2006/10/03/mini-file-uploads](http://cleanair.highgroove.com/articles/2006/10/03/mini-file-uploads)

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![vanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac8455/32.png) [@vanik](https://www.ruby-forum.com/u/vanik)\
**Post date:** [May 5, 2007, 5:45pm UTC](https://www.ruby-forum.com/t/simple-file-upload-is-failing-string-vs-file/103408/3 "2007-05-05T17:45:01Z")

</div>

Thanks Bill, that is much more knowledge than I had 2 hours ago. I may  
have a related but I don’t think identical problem.

The issue, and the article cited is that some uploads (typically  
“small”) files are treated as StringIO objects and others are TempFile  
objects. Note from my error (with the spelling error fixed) that it  
appears I’m returned a String not a StringIO object.

undefined method `content\_type’ for “scandata.txt”:String

I did change my little test from .content\_type to .read which StringIO  
does support and it also failed.  
I happen to be developing for the very first time on an OSX and I am  
going to try and repeat this on a linux system.

Gary

---

<div class="post-metadata">

**Author:** ![vanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac8455/32.png) [@vanik](https://www.ruby-forum.com/u/vanik)\
**Post date:** [May 5, 2007, 6:08pm UTC](https://www.ruby-forum.com/t/simple-file-upload-is-failing-string-vs-file/103408/4 "2007-05-05T18:08:06Z")

</div>

Stand down. I found it, and as usual it was my fault.  
The lesson here is that:

```
<%= form_tag (:action=>"save", :multipart=>"true") %>

```

is not the same as  
\<%= form\_tag ( {:action=\>“save”}, {:multipart=\>“true”}) %\>

thanks everyone
