【问题标题】:Ruby on rails, forcing the user to download a tmp fileRuby on rails,强制用户下载 tmp 文件
【发布时间】:2011-05-02 20:29:30
【问题描述】:

我在 tmp 目录中创建了一个文件,其中包含以下控制器代码:

  def download
    file_path = "#{RAILS_ROOT}/tmp/downloads/xxx.html"
    data = render_to_string( :action => :show, :layout => nil )
    File.open(file_path, "w"){|f| f << data }
    flash[:notice] = "saved to #{file_path}"
  end

这会在 tmp 目录中创建我想要的文件,我想做的是强制用户下载该文件。

在我的本地机器上,文件保存到如下路径:

/Users/xxxx/Documents/Sites/xxxx/Website/htdocs/tmp/downloads/xxxx.html

而在实时服务器上,这个网址将完全不同。

我想知道如何强制用户下载这个 xxxx.html ?

附: 如果我放一个...

redirect_to file_path

...在控制器上它只是给了我一条找不到的路线。

干杯。

【问题讨论】:

    标签: ruby-on-rails file redirect download tmp


    【解决方案1】:

    看看send_file 方法。它看起来像这样:

    send_file Rails.root.join('tmp', 'downloads', 'xxxxx.html'), :type => 'text/html', :disposition => 'attachment'
    

    :disposition => 'attachment' 将强制浏览器下载文件而不是渲染它。如果您希望它在浏览器中加载,请将其设置为“内联”。如果 nginx 在您的 Rails 应用程序前面,那么您将不得不修改您的环境配置(即,environments/production.rb):

    # For nginx:
    config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
    

    【讨论】:

      【解决方案2】:

      文件路径与 URL 很容易混淆,但这是一个重要的区别。 URL 路径为/a/b.txt 的内容实际上位于系统路径#{Rails.root}/public/a/b.txt 中,因此您可能需要同时生成两者来解决此问题。

      你可以这样解决这个问题:

      def download
        base_path = "downloads/xxx.html"
      
        system_path = File.expand_path("public/#{base_path}", Rails.root)
        url_path = "/#{base_path}"
      
        File.open(file_path, "w") do |f|
          f.puts render_to_string(:action => :show, :layout => nil)
        end
      
        flash[:notice] = "saved to #{base_path}"
      
        redirect_to(url_path)
      end
      

      您无法重定向到未通过 Web 服务器公开的资源,并且通常只有 public/ 中的内容以这种方式设置。如果您相应地配置服务器,则可以包含其他路径。

      如果您愿意,也可以通过简单地将响应呈现为可下载的内联附件来绕过整个过程:

      render(:action => :show, :layout => nil, :content_type=> 'application/octet-stream')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-08
        • 1970-01-01
        • 2011-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多