Attaching files through attachment_fu

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

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) }

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs