# How to upload a file

**URL:** <https://www.ruby-forum.com/t/how-to-upload-a-file/131816>\
**Category:** Rails\
**Created:** [March 13, 2008, 3:00pm UTC](https://www.ruby-forum.com/t/how-to-upload-a-file/131816 "2008-03-13T15:00:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pharrukh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e19b73/32.png) [@pharrukh](https://www.ruby-forum.com/u/pharrukh)\
**Post date:** [March 13, 2008, 3:00pm UTC](https://www.ruby-forum.com/t/how-to-upload-a-file/131816/1 "2008-03-13T15:00:13Z")

</div>

Hello all,

i am trying to upload a file through Ruby on Rails since last week.  
Could anyone please guide me regarding a simple upload process. Or  
guide me to a ‘working’ example/tutorial of uploading.

Waiting curiously for a respons,

Regards,

pharrukh.

---

<div class="post-metadata">

**Author:** ![pharrukh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e19b73/32.png) [@pharrukh](https://www.ruby-forum.com/u/pharrukh)\
**Post date:** [March 13, 2008, 3:51pm UTC](https://www.ruby-forum.com/t/how-to-upload-a-file/131816/2 "2008-03-13T15:51:14Z")

</div>

the super simple version:  
in your view place a form:

\<% form\_tag(…insert your path etc…), { :multipart =\> true }) do %\>  
File to Upload\<%= file\_field\_tag “file” %\>  
\<%= submit\_tag(“upload”) %\>  
\<% end %\>

then you’ll get this file in your action as params[:file] and can save  
it like this:

File.open("#{file\_location}/new\_file\_name.xxx", ‘wb’) { |f|  
f.write(params[:file].read) }

---

<div class="post-metadata">

**Author:** ![pharrukh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e19b73/32.png) [@pharrukh](https://www.ruby-forum.com/u/pharrukh)\
**Post date:** [March 13, 2008, 3:54pm UTC](https://www.ruby-forum.com/t/how-to-upload-a-file/131816/3 "2008-03-13T15:54:04Z")

</div>

On Mar 13, 2008, at 9:59 AM, pharrukh. wrote:

> pharrukh.

The important bit that’s easy to forget is the :multipart=\>true

==\> in your view new.rhtml \<==

\<% form\_for(:something, @something, :url =\> somethings\_path, :html =\>  
{ :multipart =\> true }) do |f| -%\>

Something File:  
 \<%= f.file\_field 'data' %\>

\<%= submit\_tag “Submit”, :name =\> nil %\>  
\<% end %\>

==\> in the SomethingController#create action \<==

```
 data = params[:something].delete('data') #

```

HashWithIndifferentAccess still needs the actual key type to .delete

```
 params[:dump] = "data=#{data.inspect}"#;

```

filename=#{data.original\_filename}; content\_type=#{data.content\_type}"

```
 if data.blank?
   flash[:error] = "No something file selected for upload"
   redirect_to :action => 'new' and return
 end
 content = data.read
 if content.blank?
   flash[:error] = "Selected something file was empty"
   redirect_to :action => 'new' and return
 end

```

That should get you going.

-Rob

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