# Attaching files through attachment\_fu

**URL:** <https://www.ruby-forum.com/t/attaching-files-through-attachment-fu/174000>\
**Category:** Rails\
**Created:** [August 19, 2009, 7:43am UTC](https://www.ruby-forum.com/t/attaching-files-through-attachment-fu/174000 "2009-08-19T07:43:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![charanya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5e925/32.png) [@charanya](https://www.ruby-forum.com/u/charanya)\
**Post date:** [August 19, 2009, 7:43am UTC](https://www.ruby-forum.com/t/attaching-files-through-attachment-fu/174000/1 "2009-08-19T07:43:14Z")

</div>

Hi

I wish to upload a file not from the view but from the controller  
itself.  
I have the file’s path and mimetype and size.  
I would like to know How it can be done directly from the controller

Thanks in Advance

---

<div class="post-metadata">

**Author:** ![charanya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5e925/32.png) [@charanya](https://www.ruby-forum.com/u/charanya)\
**Post date:** [August 19, 2009, 8:02am UTC](https://www.ruby-forum.com/t/attaching-files-through-attachment-fu/174000/2 "2009-08-19T08:02:40Z")

</div>

Charanya N. wrote:

> Hi
> 
> I wish to upload a file not from the view but from the controller  
> itself.  
> I have the file’s path and mimetype and size.  
> I would like to know How it can be done directly from the controller
> 
> Thanks in Advance

filepath = File.open(“File\_path”,“r”)  
File.open(path\_where\_u\_want\_to\_upload, “wb”) { |f|  
f.write(filepath.read) }

---

<div class="post-metadata">

**Author:** ![charanya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5e925/32.png) [@charanya](https://www.ruby-forum.com/u/charanya)\
**Post date:** [August 19, 2009, 10:53am UTC](https://www.ruby-forum.com/t/attaching-files-through-attachment-fu/174000/3 "2009-08-19T10:53:56Z")

</div>

On 19 Aug 2009, at 07:43, Charanya N. wrote:

> I wish to upload a file not from the view but from the controller  
> itself.  
> I have the file’s path and mimetype and size.  
> I would like to know How it can be done directly from the controller

I always prefer to actually generate an uploader object just as it  
would come from the view itself.

# Mock method to simulate an actual uploaded file

def mock\_uploader(path, type = ‘application/octet-stream’)  
uploader = ActionController::UploadedStringIO.new  
uploader.original\_path = path  
uploader.content\_type = type  
def uploader.read  
File.read(original\_path)  
end  
def uploader.size  
File.stat(original\_path).size  
end  
uploader  
end

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![charanya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5e925/32.png) [@charanya](https://www.ruby-forum.com/u/charanya)\
**Post date:** [August 19, 2009, 11:07am UTC](https://www.ruby-forum.com/t/attaching-files-through-attachment-fu/174000/4 "2009-08-19T11:07:53Z")

</div>

Thank You So much Peter  
Your Reply helped me so much.
