Forum: Ruby on Rails how to upload or download a file using ror

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3a00f53728802887ada8b8abca600947?d=identicon&s=25 Sathyanarayana Swamy (gsswamy)
on 2007-06-27 18:47
(Received via mailing list)
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
229ec425632bdc1f07be7446096393e2?d=identicon&s=25 Archana Nm (archanan21)
on 2007-08-28 08:19
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) %>
   <label for="upload_file">      Select File</label> :
   <%= file_field  'upload', 'datafile' %>
   <%= submit_tag "Upload" %>
   <%= end_form_tag %>



Archana
4c3acdc3d93f54cc7a7281780ec8a4ee?d=identicon&s=25 Shai Rosenfeld (shai)
on 2007-08-28 12:24
229ec425632bdc1f07be7446096393e2?d=identicon&s=25 Archana Nm (archanan21)
on 2007-08-29 06:23
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)
15eb647956921a6bc9655c3513feb139?d=identicon&s=25 lanzm (Guest)
on 2007-09-25 23:02
(Received via mailing list)
read the manual (api.rubyonrails.org) and perhaps try this:

in your view:

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

in the controller:

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

 end
This topic is locked and can not be replied to.