我正在使用 Rails 服务器,我可以从中下载本地存储的电影和动漫等。这是可行的,但是当我单击下载链接时,我必须刷新页面才能真正开始下载。
这是处理下载的 Controller :
class DownloadController < ApplicationController
def index
@title = params[:title]
@name = params[:name]
@path = '/media/sf_Anime,_VN,_LN/Watching, not watched yet/'+@title+'/'+@name
send_file( @path )
end
end
这是链接到该 Controller 的链接:
<% @episodes.each do |x| %>
<p> <%= x %><%= link_to " Download",
{controller: 'download', action: 'index', title: @title, name: x } %> </p>
<% end %>
编辑: 我今天做了一些测试,发现如果我尝试发送较小的文件(文本或图像),下载链接会立即起作用。我还注意到下载链接实际上也适用于电影,但下载需要 20-30 秒才能开始。 您知道什么会导致这种延迟吗?
最佳答案
您使用涡轮链接吗? Turbolinks 显然不能很好地与 send_file ( https://github.com/rails/turbolinks/issues/182 ) 配合使用。尝试在 link_to 帮助器中添加“data: { no-turbolink: true }”(或“'data-no-turbolink'=>true”),例如:
<%= link_to "Downloadable File", downloadable_file, data: { no-turbolink: true } %>
另请参阅:Rails 4, asset pipeline causes user downloadable files to be downloaded twice , rails won't send_data as file , Ruby on Rails send_file, code in controller action running twice
关于ruby-on-rails - Ruby on Rails send_file 在我刷新页面之前不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23541466/