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.
Sathyanarayana S. (Guest)
on 2007-06-27 20: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
Archana N. (Guest)
on 2007-08-28 10: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
Shai R. (Guest)
on 2007-08-28 14:24
Archana N. (Guest)
on 2007-08-29 08: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)
lanzm (Guest)
on 2007-09-26 01: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.