rails中的文件下载链接

时间:2012-10-31 18:04:28

标签: ruby-on-rails ruby-on-rails-3 hyperlink download link-to

我想让访问者可以选择下载一些pdf。 我试过了:

<%= link_to "abc", "/data/abc.pdf"%>

<%= link_to "abc", "/data/abc.pdf", :format => 'pdf' %>

和一些变化,但它们似乎不起作用。我一直在No route matches [GET] "/data/abc.pdf"

我将pdf文件放在名为data的文件夹中,该文件夹位于assets文件夹中。 任何帮助将不胜感激。

6 个答案:

答案 0 :(得分:74)

Rails 4:

路线中的

get "home/download_pdf"

在控制器中(已经有pdf):

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

(需要生成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

在视图中:

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

答案 1 :(得分:67)

Rails 4:

路线中的

get "home/download_pdf"

在控制器中(已经有pdf):

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

(需要生成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

在视图中:

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

Rails 3

做到这一点的方法:

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

您应该选择

Rails&lt; 3

公用文件夹中的文件

这可能是你的答案:How to download a file from rails application

您应该将文件放在公共文件夹中,这就是诀窍。

正确放置文件时应该有效。

如果您无法将文件移至公共文件夹,请与我们联系。

通过控制器下载

使用下载操作和link_to

创建一个控制器
  def download
    send_file '/assets/data/abc.pdf', :type=>"application/pdf", :x_sendfile=>true
  end

答案 2 :(得分:17)

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.

答案 3 :(得分:3)

你可以像这样简单地调用你的控制器动作

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

并在您的控制器中

def download_file
 redirect_to paperclip_attachment.file.url
end

答案 4 :(得分:1)

我努力寻找从公共目录自动下载某些文件的简单方法。 最后我想出了这个解决方案。 例如: 我将我的文件放在公共目录中的SVG文件夹中。

Public/svg/Test1.xlsx

现在,当我尝试访问它时加载它并使用Paper clip给出Path它会产生问题。 即使我尝试完整路径它也会给出问题所以我们可以使它成为动态路径。 首先获取主机的路径,以便可以轻松地重定向。     &lt;%url = request.original_url.chomp(request.fullpath)%&gt;

现在我们可以访问公共文件夹中的任何文件,如下所示 并传递ID和下载选项。 下载选项重命名您要下载的任何文件。

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

现在点击链接已准备好我们可以点击上面链接下载文件。 使用以下脚本自动下载文件。

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

对于PDF或任何其他任何文件类型的情况,只需要更改文件扩展名。

答案 5 :(得分:1)

对我来说,链接到pdf文件的最佳解决方案是通过以下简单步骤:

1-将pdf文件添加到公用文件夹

2-`<%=链接到“ /nameofthefile.pdf”,目标:“ _blank”执行%> pdf文件<%end%>

以下是视频,详细介绍了https://www.youtube.com/watch?v=bNZ6FJrZ_lo&t=64s`