【问题标题】:Zero Bytes Sent When Sending Axlsx Gem Generated File From Sinatra从 Sinatra 发送 Axlsx Gem 生成的文件时发送的零字节
【发布时间】:2018-12-04 23:48:59
【问题描述】:

尝试使用 Ruby Sinatra 和 AXLSX gem 提示下载窗口并流式传输 XLSX 文件,我的 excel 文件成功序列化到本地文件,所以我知道它是一个有效的 excel 文档,但我需要它来传输内容到最后用户。没有任何在线文档包含 AXLS 和 Sinatra 一起使用的示例,只有导轨。感谢您的帮助!

class Downloads < Sinatra::Base
  get '/downloads/report' do
    ## ...
    Axlsx::Package.new do |p|
      p.workbook.add_worksheet(name: 'tab name') do |sheet|
        ## ...
      end
      content_type 'application/xlsx'
      attachment 'cost-code-dashboard.xlsx'
      p.to_stream # unsuccessful
      # p.to_stream.read # unsuccessful as well
    end
  end
end

我也试过下面的sn-p不成功

Axlsx::Package.new do |p|
  ## ...
  send_file p.to_stream.read, type: "application/xlsx", filename: "cost-code-dashboard.xlsx"
end

【问题讨论】:

    标签: ruby sinatra rack axlsx


    【解决方案1】:

    问题似乎与Axlsx::Package.new 的调用方式有关,辅助函数在 Axlsx 中不可用,以下解决方案有效 - 在线文档说以下 content_type 更好

    get '/downloads' do
      content_type :'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    
      p = Axlsx::Package.new
      p.workbook.add_worksheet(name: 'Test') do |sheet|
        sheet.add_row ['Hello world']
      end
      p.to_stream
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-18
      • 1970-01-01
      • 2011-09-03
      • 2013-11-07
      • 1970-01-01
      • 2011-07-07
      • 1970-01-01
      相关资源
      最近更新 更多