# File upload gives filename as String, no IO object

**URL:** <https://www.ruby-forum.com/t/file-upload-gives-filename-as-string-no-io-object/115376>\
**Category:** Rails\
**Created:** [September 24, 2007, 4:09pm UTC](https://www.ruby-forum.com/t/file-upload-gives-filename-as-string-no-io-object/115376 "2007-09-24T16:09:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Siemen\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@Siemen\_B](https://www.ruby-forum.com/u/Siemen_B)\
**Post date:** [September 24, 2007, 4:09pm UTC](https://www.ruby-forum.com/t/file-upload-gives-filename-as-string-no-io-object/115376/1 "2007-09-24T16:09:29Z")

</div>

Hi list,

I’m having a headache with file upload in rails…

In short, the problem boils down to the upload form giving me a String  
containing the file name of the uploaded file instead of the IO object  
it should give me to access the file’s content and MIME type.

I have checked [http://railsforum.com/viewtopic.php?id=4642&p=1](http://railsforum.com/viewtopic.php?id=4642&p=1) and  
[Peak Obsession](http://manuals.rubyonrails.com/read/chapter/77), but the problem seems  
not to occur there… I also have spent quite some time searching  
google and this list on this issue, but nothing that succeeded to  
enlighten me came up…

I’m using rails v. 1.2.1.

I have a view with a file upload form:

\<% form\_tag :action =\> ‘create’, :multipart =\> true do %\>  
\<%= file\_field(‘photo’, ‘uploaded\_file’) %\>  
\<%= submit\_tag “Create” %\>  
\<% end %\>

Which renders to

The database model has no column named ‘uploaded\_file’ because this  
value is to be split into a field for the MIME and one for the  
content. So I added a setter method:

class Photo \< ActiveRecord::Base  
def uploaded\_file=(file)  
self.mime = file.content\_type # =\> Raises undefined method  
`content\_type’ for “Photo.jpg”:String  
end  
end

The request parameters from rails’ traceback are:

Parameters: {“photo”=\>{“creator”=\>“Siemen”,  
“uploaded\_file”=\>“Photo.jpg”}, “multipart”=\>“true”,  
“commit”=\>“Create”}

So also a string here…

Any help would be greatly appreciated!

Siemen

---

<div class="post-metadata">

**Author:** ![Siemen\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@Siemen\_B](https://www.ruby-forum.com/u/Siemen_B)\
**Post date:** [September 24, 2007, 4:43pm UTC](https://www.ruby-forum.com/t/file-upload-gives-filename-as-string-no-io-object/115376/2 "2007-09-24T16:43:34Z")

</div>

Hi,

> I’m having a headache with file upload in rails…
> 
> In short, the problem boils down to the upload form giving me a String  
> containing the file name of the uploaded file instead of the IO object  
> it should give me to access the file’s content and MIME type.

did you set tour form to send multipart encoding? If not, that’s the  
problem

regards,

javier ramírez

---

<div class="post-metadata">

**Author:** ![Siemen\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@Siemen\_B](https://www.ruby-forum.com/u/Siemen_B)\
**Post date:** [September 24, 2007, 5:02pm UTC](https://www.ruby-forum.com/t/file-upload-gives-filename-as-string-no-io-object/115376/3 "2007-09-24T17:02:37Z")

</div>

On Sep 24, 2007, at 10:08 AM, Siemen B. wrote:

> not to occur there… I also have spent quite some time searching  
> \<%= submit\_tag “Create” %\>  
> \<% end %\>

Try this:  
\<% form\_tag({:action =\> ‘create’}, :multipart =\> true) do %\>

The multipart option is not part of the url\_for options so you have  
to make them explicitly separate.

> value is to be split into a field for the MIME and one for the
> 
> Parameters: {“photo”=\>{“creator”=\>“Siemen”,  
> “uploaded\_file”=\>“Photo.jpg”}, “multipart”=\>“true”,  
> “commit”=\>“Create”}
> 
> So also a string here…
> 
> Any help would be greatly appreciated!
> 
> Siemen

In your controller, you probably want something like:

def create  
data = params[:photo].delete(‘uploaded\_file’) #  
HashWithIndifferentAccess still needs the actual key type to .delete

```
 @photo = Photo.new(params[:photo])

 if data.blank?
   flash[:error] = "No image file selected for upload"
   redirect_to :action => 'new' and return
 end
 content = data.read
 if content.blank?
   flash[:error] = "Selected upload file was empty"
   redirect_to :action => 'new' and return
 end
 # you can then do stuff with the content or with:
 # filename = data.original_filename
 # content_type = data.content_type

 # eventually,
 if @photo.save
 # OK
 else
 # too bad
 end

```

end

I might have missed something that you want to do, but this ought to  
at least get you started.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Siemen\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@Siemen\_B](https://www.ruby-forum.com/u/Siemen_B)\
**Post date:** [September 24, 2007, 5:16pm UTC](https://www.ruby-forum.com/t/file-upload-gives-filename-as-string-no-io-object/115376/4 "2007-09-24T17:16:42Z")

</div>

> did you set tour form to send multipart encoding? If not, that’s the problem

I thought sI had

> > \<% form\_tag :action =\> ‘create’, :multipart =\> true do %\>

However, this results in the multipart=true-thing being included in  
the url, not as an attribute of the form tag:

> >

Using more explicit () and {} solved it:

\<% form\_tag({:action =\> ‘create’}, {:multipart =\> true}) do %\>

gives me the correct form:

Thank you very much for the helpful answer!

Siemen

---

<div class="post-metadata">

**Author:** ![Siemen\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7bcc69/32.png) [@Siemen\_B](https://www.ruby-forum.com/u/Siemen_B)\
**Post date:** [September 24, 2007, 5:20pm UTC](https://www.ruby-forum.com/t/file-upload-gives-filename-as-string-no-io-object/115376/5 "2007-09-24T17:20:43Z")

</div>

On Sep 24, 5:01 pm, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > it should give me to access the file’s content and MIME type.
> 
> > The database model has no column named ‘uploaded\_file’ because this  
> > The request parameters from rails’ traceback are:
> 
> ```
> redirect_to :action => 'new' and return
> # eventually,
> if @photo.save
> # OK
> else
> # too bad
> end
> 
> ```
> 
> end
> 
> I might have missed something that you want to do, but this ought to  
> at least get you started.

Yes, definitely. Thanks!

- Siemen
