Speed up development with full-stack environments for every branch.

Learn More

How to download a file using Ruby-on-Rails?

1334 Runs 12352 Views 4201 Copies

This example demostrate how to download a file using Ruby-on-Rails.

Saved

Saved

rogalvil 36

rogalvil
published 4 years ago

=README

==How to download a file using Ruby-on-Rails?
This example demostrate how to download a file using Ruby-on-Rails.

==Steps to replicate this example
1. Run 'bundle install'
2. Run 'rails g controller download index zip pdf doc'
4. Modify routes.rb add this lines
  root 'download#index'
5. Modify index.html.erb in hello view folder and copy content of index.html.erb
6. Create private folder and create 3 files:
  Rogelio Alvarado Vilchis.zip
  Rogelio Alvarado Vilchis.pdf
  Rogelio Alvarado Vilchis.docx
7. Modify download_controller.rb add these lines
  def zip
    send_file Rails.root.join('private', 'Rogelio Alvarado Vilchis.zip'), :type=>"application/zip", :x_sendfile=>true
  end

  def pdf
    send_file Rails.root.join('private', 'Rogelio Alvarado Vilchis.pdf'), :type=>"application/pdf", :x_sendfile=>true
  end

  def doc
    send_file Rails.root.join('private', 'Rogelio Alvarado Vilchis.docx'), :type=>"application/doc", :x_sendfile=>true
  end
8. Press button Run


Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).