How to upload or download a file using ror


#1

Hi friends,

    i am a fresher to ruby on rails . i need sample code/program

of how to download or upload files,and i also want code to stage a
file instead of posting it to a webserver.

i would be thankful if u can give me this information


#2

Sathyanarayana Swamy wrote:

Hi friends,

    i am a fresher to ruby on rails . i need sample code/program

of how to download or upload files,and i also want code to stage a
file instead of posting it to a webserver.

i would be thankful if u can give me this information

Hi,

This works for Uploading File.

In Controller:

def uploadFile
if(@params[‘commit’] ==“Upload”)
path=“C:\Intranet\directory\public\data\file”
File.open(path,“wb”) {|f|
f.write(@params[:upload][:datafile].read)}
render :text =>“File has been uploaded successfully”
end
end

In rhtml

<%= start_form_tag ({:action => ‘uploadFile’}, :multipart => true) %>
Select File :
<%= file_field ‘upload’, ‘datafile’ %>
<%= submit_tag “Upload” %>
<%= end_form_tag %>

Archana


#3

Uploads:

http://uploadcolumn.rubyforge.org/

Downloads: ( send_file )

http://api.rubyonrails.org/classes/ActionController/Streaming.html#M000091


#4

read the manual (api.rubyonrails.org) and perhaps try this:

in your view:

Fileupload <%= start_form_tag({:action => 'upload_file'}, :multipart => true) %> <%= file_field_tag("uploaded_file")%> <%= submit_tag('Save')%> <%= end_form_tag %>

in the controller:

def upload_file
file_content = “”
file_io = params[:uploaded_file]
file_io.each_line {|line|
}
render :text => “duh…”

end


#5

Sathyanarayana Swamy wrote:

Hi friends,

    i am a fresher to ruby on rails . i need sample code/program

of how to download or upload files,and i also want code to stage a
file instead of posting it to a webserver.

i would be thankful if u can give me this information

For Downloading use Send_file

In controller,

send_file(@path,
:filename => params[:filename] ,
:type => ‘application/octet-stream’,
:disposition => “attachment”,
:streaming => true,
:buffer_size => 4096)