【问题标题】:Using send_file in rails在 Rails 中使用 send_file
【发布时间】:2017-08-17 16:00:16
【问题描述】:

我正在使用 rails 发送一个 ms-word 文件。 即当我单击链接时,将发送来自 tmp 文件夹(在项目中)的 doc 文件。

我使用的代码是

@filename ="#{RAILS_ROOT}/tmp/test/test.doc"
send_file(@filename ,
            :filename      =>  "test",
            :type          =>  'application/msword',
            :disposition  =>  'attachment',
            :streaming    =>  'true',
        :buffer_size  =>  '4096')

它正在工作,但它正在发送一个空文件。文件中缺少内容。有什么建议吗?

【问题讨论】:

  • +1。确保您没有发送空文件。

标签: ruby-on-rails


【解决方案1】:

没有send_file :streaming 选项,它是 :stream。您正在传递错误的参数类型。 :buffer_size 应该是数字,而不是字符串。 :stream 应该是布尔值,而不是字符串。

:stream => true,
:buffer_size => 4096,

您只需要文件名参数(如果您想发送与原始名称不同的文件)。您使用的其他选项是默认选项(除了 :type)。

你可以试试这个吗?

@filename ="#{RAILS_ROOT}/tmp/test/test.doc"
send_file(@filename, :filename => "test.doc")

【讨论】:

    【解决方案2】:

    注释掉config/environments/production.rb中的以下行

    config.action_dispatch.x_sendfile_header = "X-Sendfile"
    

    【讨论】:

    • 对于 nginx,它是 X-Accel-Redirect
    【解决方案3】:
    In your view =>
    <%= link_to "click here to download", signed_feeds_pdf_path(:feed_image_path => feed_image.feedimage.path), target: '_self' %>
    In your controller =>
      def pdf
        file_name = params[:feed_image_path].split('/').last
        @filename ="#{Rails.root}/public/uploads/feed_image/feedimage/#{file_name}"
        send_file(@filename ,
          :type => 'application/pdf/docx/html/htm/doc',
          :disposition => 'attachment')           
      end
    

    【讨论】:

      【解决方案4】:

      尝试使用 :disposition => 'inline' 发送

      send_file path, :type => 'application/msword', :disposition => 'inline'
      

      【讨论】:

        猜你喜欢
        • 2011-06-05
        • 1970-01-01
        • 2015-03-16
        • 1970-01-01
        • 1970-01-01
        • 2012-01-12
        • 2011-04-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多