Rails send_data用于移动设备

时间:2014-02-13 19:38:03

标签: android ruby-on-rails ruby ruby-on-rails-3 spreadsheet

我正在尝试生成* .xls(电子表格文件),并可以下载移动设备。

这适用于桌面浏览器和iphone,但android会告诉该文件无法下载。

controller.rb

def index
   #find the Object
   respond_to do |format|
      ...         
      format.xls { send_data as_xls, content_type: 'application/vnd.ms-excel', filename: 'data.xls' }
   end
end

def as_xls(options = {})
   require 'spreadsheet'
   Spreadsheet.client_encoding = 'UTF-8'

   book = Spreadsheet::Workbook.new
   #only some work to create the sheet  
   ...
   require 'stringio'
   data = StringIO.new
   book.write data
   data.rewind
   #data.read
   data.string.bytes.to_a.pack("C*")
end

view.html.erb

model_name_path(@model, format: "xls")

版本

Ruby 1.9.3

Rails 3.2.13

0 个答案:

没有答案