file download link in rails

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 18:35:28

Rails 4:

in routes:

get "home/download_pdf"

in controller (already have pdf):

def download_pdf
  send_file(
    "#{Rails.root}/public/your_file.pdf",
    filename: "your_custom_file_name.pdf",
    type: "application/pdf"
  )
end

in controller (need to generate pdf):

require "prawn"
class ClientsController < ApplicationController

  def download_pdf
    client = Client.find(params[:id])
    send_data generate_pdf(client),
              filename: "#{client.name}.pdf",
              type: "application/pdf"
  end

  private

  def generate_pdf(client)
    Prawn::Document.new do
      text client.name, align: :center
      text "Address: #{client.address}"
      text "Email: #{client.email}"
    end.render
  end
end

in view:

<%= link_to 'Download PDF', home_download_pdf_url %>

Rails 3

The way to do it:

def download
  send_data pdf,
    :filename => "abc.pdf",
    :type => "application/pdf"
end

You should go to this alternative

Rails < 3

File in public folder

This may the the answer to you: How to download a file from rails application

You should place your file in public folder, that is the trick.

Should work when the file is placed correctly.

Let me know if you can't move your file to public folder.

Download via controller

Create a controller with a downlaod action and link_to it

  def download
    send_file '/assets/data/abc.pdf', :type=>"application/pdf", :x_sendfile=>true
  end

Rails 4:

in routes:

get "home/download_pdf"

in controller (already have pdf):

def download_pdf
  send_file(
    "#{Rails.root}/public/your_file.pdf",
    filename: "your_custom_file_name.pdf",
    type: "application/pdf"
  )
end

in controller (need to generate pdf):

require "prawn"
class ClientsController < ApplicationController

  def download_pdf
    client = Client.find(params[:id])
    send_data generate_pdf(client),
              filename: "#{client.name}.pdf",
              type: "application/pdf"
  end

  private

  def generate_pdf(client)
    Prawn::Document.new do
      text client.name, align: :center
      text "Address: #{client.address}"
      text "Email: #{client.email}"
    end.render
  end
end

in view:

<%= link_to 'Download PDF', home_download_pdf_url %>

If the files are static (meaning they don't change), place them in the public folder.

Then you can download like

<a href="file.pdf" download>PDF</a>

or with ERB

<%= link_to 'PDF', 'file.pdf', download: '' %>

and to give the file another name for downloading, just pass that name to the download option

<%= link_to 'PDF', 'file.pdf', download: 'data' %>

This will download the file as data.pdf instead of file.pdf.

you can simply call your controller action like this

<%= link_to "Download", download_file_path, class: "btn btn-sm btn-default", target: "_blank" %>

and in your controller

def download_file
 redirect_to paperclip_attachment.file.url
end

I Struggle a lot to find simple way to Auto Downlaod Some File From Public Directory. Finally i come up with this solution. For Example: i Have my file in SVG folder inside Public Directory.

Public/svg/Test1.xlsx

Now When i try to Access it load it and Give Path with Paper clip it give issue. Even When i try full path it give issue as well so we can make it dynamic path. First Get Path of the Host so that Can Redirect Easily. <% url = request.original_url.chomp(request.fullpath) %>

Now we Can Access any file in Public Folder Like below and Pass id and Download option. Download option rename any file which u want to download.

<%= link_to 'Database File', "#{url}/svgs/Test1.xlsx", download: 'Data.xlsx',id: "Link_to_Downlaod" %>

Now Click able link is Ready We Can Click on Above link to Download the File. Use the Following Script to Auto Download the File.

  <script type="text/javascript">
    window.onload = document.getElementById('Link_to_Downlaod').click();
  </script>
</div>

For the Case of PDF or any Any other file type just need to change the file extension.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!