【问题标题】:send_file just sends an empty filesend_file 只是发送一个空文件
【发布时间】:2011-03-28 05:40:54
【问题描述】:

我正在寻找一种下载 xml 文件的方法。我用:

file_path = 'folder/' + xml_name + '.xml'
send_file file_path, :type => "text/xml"

但这总是给我下载一个空文件。该文件本身包含 16 KB 的数据...

这是为什么呢?

前知

【问题讨论】:

    标签: ruby-on-rails sendfile


    【解决方案1】:

    可能你要注释掉

    config.action_dispatch.x_sendfile_header = "X-Sendfile"

    生产中.rb

    请参阅http://vijaydev.wordpress.com/2010/12/15/rails-3-and-apache-x-sendfile/ 了解说明

    【讨论】:

    • 如果你使用的是 nginx,你应该使用:config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
    • 这个答案对我有用,但我认为安装/启用 mod_xsendfile 将是首选解决方案。
    【解决方案2】:

    问题已保存,但我不知道为什么

    File.open(file_path, 'r') do |f|
      send_data f.read, :type => "text/xml", :filename => "10.xml"
    end
    

    send_data 正在工作...但 send_file 没有!

    【讨论】:

    • 没有为我工作..两者在产品中都有相同的问题..我检查了产品的配置并且已禁用 x_sendfile_header
    【解决方案3】:

    正如 Eugene 在他的回答中所说,在生产环境中,Rails 将让 Apache 或 nginx 使用 x-sendfile 为您发送实际文件,如果您不使用其中任何一个作为 rails 的基础架构,则必须注释掉

    中建议的行

    config/environments/production.rb 文件。

    # config.action_dispatch.x_sendfile_header = "X-Sendfile"
    

    【讨论】:

    • +1 用于实际解释为什么要删除此标头。在 Heroku 上,我使用的是 Unicorn。
    【解决方案4】:

    您必须在./config/environments/production.rb 中启用 sendfile 使用:

    config.action_dispatch.x_sendfile_header = "X-Sendfile"
    

    如果此行不存在(或被注释掉),那么 Rails 将正确发送文件,但不会通过 Apache。

    如果您获得的是 0 字节文件,请确保您已安装 mod_xsendfile,它可从 https://tn123.org/mod_xsendfile 获得

    下载单个源文件(mod_xsendfile.c)并编译它(apxs -cia mod_xsendfile.c)。您可能希望以 root 身份运行 apxs,以便正确设置所有内容。

    然后,您需要在 Apache 配置文件中设置 XSendFileXSendFilePath 选项。有关详细信息,请参阅上述 URL 中的帮助。

    【讨论】:

      【解决方案5】:

      在我的情况下,同样的事情发生了。 我正在发送文件并将其删除。
      像这里

      File.open(file_path, 'r') do |f|      
        send_data f.read, filename: 'my_file.docx' , type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', disposition: 'attachment'
      end
      File.delete(file)
      

      但是

      filename: 'my_file.docx'
      

      拯救我的一天

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-13
        • 2018-10-27
        • 1970-01-01
        • 2017-09-24
        • 2011-06-18
        • 2017-03-01
        • 2011-04-13
        • 1970-01-01
        相关资源
        最近更新 更多