Ruby on Rails的send_file需要永远显示下载对话框

时间:2023-02-06 00:05:10

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

所以我有一个~40MB .wav文件,用户只需点击一下即可下载。标记看起来像这样:

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

艺术品下载工作正常,它只有一个~2MB的文件,但.wav文件只需要20秒左右的时间来显示下载对话框。有什么问题?我只是希望用户能够点击“下载”并下载。

2 个解决方案

#1


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?

文件是公共的,并且控制器中没有明显的逻辑需要使用它...为什么不直接链接到文件,所以网络服务器可以处理它,而不是通过rails进行往返,如此?

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

#2


0  

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

您必须配置服务器以将其类型设置为application / octet-stream,否则浏览器将尝试处理它。

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

我不确定你的x_sendfile代码,所以你应该先试试它。

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

你检查过申请日志了吗? log / development.log包含有用的信息。

Also try using redirect_to the resource instead.

也可以尝试使用redirect_to资源。

#1


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?

文件是公共的,并且控制器中没有明显的逻辑需要使用它...为什么不直接链接到文件,所以网络服务器可以处理它,而不是通过rails进行往返,如此?

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

#2


0  

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

您必须配置服务器以将其类型设置为application / octet-stream,否则浏览器将尝试处理它。

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

我不确定你的x_sendfile代码,所以你应该先试试它。

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

你检查过申请日志了吗? log / development.log包含有用的信息。

Also try using redirect_to the resource instead.

也可以尝试使用redirect_to资源。