2

So I have a ~40MB .wav file that users are downloading with the click of a button. The markup looks like this:

            <div class="row">
                <div class="col-md-6">
                    <%= link_to "Download Single", download_song_path, class: "btn btn-default btn-custom" %>
                </div>
                <div class="col-md-6">
                    <%= link_to "Download Artwork", download_artwork_path, class: "btn btn-default btn-custom" %>
                </div>
            </div>

And the controller is this:

class WelcomeController < ApplicationController
    def index
    end

  def download_song
    send_file "#{Rails.root}/public/white-flame.wav", :x_sendfile => true, :type=>"audio/wav", :filename => "white-flame.wav"
  end

  def download_artwork
    send_file "#{Rails.root}/public/white-flame-artwork.jpg", :x_sendfile => true, :type=>"image/jpg", :filename => "white-flame.jpg"
  end
end

The artwork download works fine, it's only a ~2MB file, but the .wav file literally takes 20 seconds or so just for the download dialog to display. What's the issue? I just want the user to be able to click "Download" and it download.

trevorhinesley
  • 845
  • 1
  • 10
  • 36

2 Answers2

0

The files are public, and there is no obvious logic in the controller which requires its use... Why not link directly to the file, so the webserver can handle it, instead of taking a round trip through rails, like so?

        <div class="row">
            <div class="col-md-6">
                <%= link_to "Download Single", 'white-flame.wav', class: "btn btn-default btn-custom" %>
            </div>
            <div class="col-md-6">
                <%= link_to "Download Artwork", 'white-flame-artwork.jpg', class: "btn btn-default btn-custom" %>
            </div>
        </div>
Brad Werth
  • 17,411
  • 10
  • 63
  • 88
0

You have to configure your server to set their type as application/octet-stream or the browser will try to handle it.

I'm not sure about your x_sendfile code, so you should try it without that first.

Have you checked your application logs? log/development.log contains useful information.

Also try using redirect_to the resource instead.

tadman
  • 208,517
  • 23
  • 234
  • 262