# Upload files to the specified directory

**URL:** <https://www.ruby-forum.com/t/upload-files-to-the-specified-directory/103872>\
**Category:** Rails\
**Created:** [May 11, 2007, 5:22am UTC](https://www.ruby-forum.com/t/upload-files-to-the-specified-directory/103872 "2007-05-11T05:22:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhaval](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@dhaval](https://www.ruby-forum.com/u/dhaval)\
**Post date:** [May 11, 2007, 5:22am UTC](https://www.ruby-forum.com/t/upload-files-to-the-specified-directory/103872/1 "2007-05-11T05:22:18Z")

</div>

hello

Can you tell me how do i create a page that allows me to have a facility  
by which the user can upload a file to the defined folder

pls provide the code for controller,model and view

thanks

dhaval

---

<div class="post-metadata">

**Author:** ![dhaval](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@dhaval](https://www.ruby-forum.com/u/dhaval)\
**Post date:** [May 11, 2007, 7:20am UTC](https://www.ruby-forum.com/t/upload-files-to-the-specified-directory/103872/2 "2007-05-11T07:20:25Z")

</div>

dhaval wrote:

> hello
> 
> Can you tell me how do i create a page that allows me to have a facility  
> by which the user can upload a file to the defined folder
> 
> pls provide the code for controller,model and view
> 
> thanks
> 
> dhaval

Controller action:

def upload  
File.open  
“#{RAILS\_ROOT}/uploads/#{params[:file].original\_filename}”, ‘wb’ do |f|  
f.write(params[:file].read)  
end  
redirect\_to :action =\> ‘index’  
end

View:

\<% form\_tag({:action =\> ‘upload’}, {:multipart =\> true}) do %\>  
Upload a file:  
\<%= file\_field\_tag ‘file’ %\>  
\<%= submit\_tag ‘Submit’ %\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![dhaval](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@dhaval](https://www.ruby-forum.com/u/dhaval)\
**Post date:** [May 11, 2007, 8:34am UTC](https://www.ruby-forum.com/t/upload-files-to-the-specified-directory/103872/3 "2007-05-11T08:34:22Z")

</div>

thanx a lot man 🙂
