Ruby-on-rails rails中的文件下载链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13164063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
file download link in rails
提问by Ionut Hulub
I would like to give visitors the option to download some pdf. I have tried:
我想让访问者可以选择下载一些 pdf。我试过了:
<%= link_to "abc", "/data/abc.pdf"%>
<%= link_to "abc", "/data/abc.pdf", :format => 'pdf' %>
and some variations but they don't seem to work. I keep getting No route matches [GET] "/data/abc.pdf"
和一些变化,但它们似乎不起作用。我不断得到No route matches [GET] "/data/abc.pdf"
I have the pdf files in a folder called data, located in the assets folder. Any help would be appreciated.
我将 pdf 文件放在名为 data 的文件夹中,该文件夹位于资产文件夹中。任何帮助,将不胜感激。
回答by felipeclopes
Rails 4:
导轨 4:
in routes:
在路线中:
get "home/download_pdf"
in controller (already have pdf):
在控制器中(已经有 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):
在控制器中(需要生成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
导轨 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
导轨 < 3
File in public folder
公用文件夹中的文件
This may the the answer to you: How to download a file from rails application
这可能就是你的答案:如何从 rails 应用程序下载文件
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_toit
创建一个带有下载操作的控制器,link_to它
def download
send_file '/assets/data/abc.pdf', :type=>"application/pdf", :x_sendfile=>true
end
回答by rusllonrails
Rails 4:
导轨 4:
in routes:
在路线中:
get "home/download_pdf"
in controller (already have pdf):
在控制器中(已经有 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):
在控制器中(需要生成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 %>
回答by Cruz Nunez
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
或与 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.pdfinstead of file.pdf.
这将下载文件data.pdf而不是file.pdf.
回答by Muhammad Nasir Shamshad
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
回答by Hafiz Abdul Rehman
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.
我一直在努力寻找从公共目录自动下载某些文件的简单方法。最后我想出了这个解决方案。例如:我的文件位于公共目录内的 SVG 文件夹中。
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) %>
现在,当我尝试访问它时,加载它并使用回形针给出路径,它会出现问题。即使当我尝试完整路径时,它也会出现问题,因此我们可以使其成为动态路径。首先获取主机的路径,以便可以轻松重定向。<% 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.
现在我们可以访问公共文件夹中的任何文件,如下所示,并通过 ID 和下载选项。下载选项重命名您要下载的任何文件。
<%= 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.
对于 PDF 或任何其他文件类型,只需更改文件扩展名。
回答by nourza
For me the best solution to link to a pdf file is by these simple steps:
对我来说,链接到 pdf 文件的最佳解决方案是通过以下简单步骤:
1- Add your pdf file inside the public folder
1-将您的pdf文件添加到公共文件夹中
2- `<%= link_to "/nameofthefile.pdf",target: "_blank" do %>pdf file<% end %>
2- `<%= link_to "/nameofthefile.pdf",target: "_blank" do %>pdf file<% end %>
Here is video to show you this by details https://www.youtube.com/watch?v=bNZ6FJrZ_lo&t=64s`
这是视频,通过详细信息向您展示这一点https://www.youtube.com/watch?v=bNZ6FJrZ_lo&t=64s`

