2

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 %>

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?

user3551655
  • 21
  • 1
  • 3

4 Answers4

4

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 } %>

See also: 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

Community
  • 1
  • 1
k8ydid8
  • 41
  • 2
0

Edited to reflect comment below. I would simple add a concern for handling downloads and then use

include Concerns::Downloads

to handle your download request. the routes.rb would look like this.

resources :movies do
member do
  post 'download'
end

and in the view

  <%= link_to 'Download', {:controller => 'movies', :action => 'download'}, {:method => :post } %></a>
DMH
  • 2,529
  • 3
  • 24
  • 35
  • I tried moving the send_file section into a method, that didn't change anything though :/ but thanks! – user3551655 May 08 '14 at 13:45
  • @user3551655 Having looked further I would not have a Downloads controller, but instead create a concern within your movies controller which handles the downloading of your movies. – DMH May 08 '14 at 13:58
  • updated my post with some tests i did today. Sorry about not responding earlier. – user3551655 May 09 '14 at 21:13
0

Move the file to public folder
add only file name into link_to

<%= link_to "Downloadable File", "/"+filename, %>
subbiah
  • 55
  • 6
-1

Try setting the disposition to attachment in send_file:

class DownloadController < ApplicationController
  def index
    ...
    send_file( @path, :disposition => 'attachment' )
  end
end

The issue may be that your browser is trying to open the file itself - :disposition => 'attachment' prompts the browser to download the file, even if it thinks the file is something that it can open.

DaveMongoose
  • 875
  • 9
  • 15