直到刷新页面,Ruby on Rails send_file才起作用? [英] Ruby on Rails send_file doesn't work until i refresh the page?

查看:115
本文介绍了直到刷新页面,Ruby on Rails send_file才起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Rails服务器,可以从中下载本地存储的电影和动画等.这是一种工作方式,但是当我单击下载链接时,我必须刷新页面才能真正开始下载.

I am working on a Rails server which I can download my locally stored movies and anime etc from. This is kind of working but when I click the download link I have to refresh the page in order for the download to actually start.

这是处理下载的控制器:

This is the controller that handles the download:

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

这是链接到该控制器的链接:

and this is the link that links to that controller:

<% @episodes.each do |x| %>
<p> <%= x %><%= link_to " Download", 
    {controller: 'download', action: 'index', title: @title, name: x } %> </p>
<% end %>

修改: 我今天进行了一些测试,发现如果尝试发送较小的文件(文本或图像),下载链接将立即起作用.我还注意到下载链接实际上也适用于电影,但是开始下载需要20到30秒. 您是否知道会导致这种延迟的原因?

edit: I did some testing today and noticed that the download links work instantly if i try to send a smaller file (text or image). I also noticed that the download links actually works for the movies aswell but it takes 20-30 seconds for the download to start. Do you have any idea of what would cause this delay?

推荐答案

您是否正在使用涡轮链接? Turbolink显然不能与send_file很好地配合使用( https://github.com/rails/turbolinks/Issues/182 ).尝试在link_to助手中添加数据:{no-turbolink:true}"(或'data-no-turbolink'=> true"),例如:

Are you using turbolinks? Turbolinks apparently doesn't play nicely with send_file (https://github.com/rails/turbolinks/issues/182). Try adding "data: { no-turbolink: true }" (or "'data-no-turbolink'=>true") in the link_to helper, e.g.:

<%= link_to "Downloadable File", downloadable_file, data: { no-turbolink: true } %>

另请参阅:路轨4 ,资产管道会导致用户可下载文件被下载两次 rails不会send_data作为文件 Ruby on Rails send_file,控制器操作中的代码运行两次

这篇关于直到刷新页面,Ruby on Rails send_file才起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆