加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Ruby on Rails中的send_data与Spreadsheet插件

发布时间:2020-12-17 04:19:22 所属栏目:百科 来源:网络整理
导读:我在控制器中有一个功能,它接受一些规范并生成一个报告.在视图中调用此函数user_report: %= submit_to_remote’submit-button’,“将报告导出到Excel”,:url = {:controller = :reports,:action = :user_report,:print_state = ‘print’}% 在report
我在控制器中有一个功能,它接受一些规范并生成一个报告.在视图中调用此函数user_report:

< %= submit_to_remote’submit-button’,“将报告导出到Excel”,:url => {:controller => :reports,:action => :user_report,:print_state => ‘print’}%>

在reports_controller中,我使用Spreadsheet插件在user_report函数中生成Excel文件.我想使用send_data将文件流式传输给用户,而不是先在服务器上创建它.我所做的研究表明,使用StringIO是可行的方法,如下所示.令人沮丧的是,当我调用send_data时没有任何反应.该插件似乎可以很好地创建一个文件并将其保存在服务器上,但在使用send_file时什么都不做,这表明问题不在于插件.但是我对send_file / send_data做错了什么?

该函数本身如下所示:

def user_report

if request.post?
  unless params[:reports][:userid].blank?
    @userid=params[:reports][:userid]
  end
  if params[:print_state]=='print'  

    report = Spreadsheet::Workbook.new
    info = report.create_worksheet :name => 'User Information'
    info.row(1).push 'User ID',@userid

    @outfile = "Report_for_#{@userid}.xls"

    require 'stringio'
    data = StringIO.new ''
    report.write data
    send_data data.string,:type=>"application/excel",:disposition=>'attachment',:filename => @outfile
  end

  respond_to do |format|
    format.js { }
  end
end

end

日志文件读取
2010-10-18 14:13:59 INFO – 发送数据Report_for_jjohnson.xls
但是没有在浏览器中开始下载.我之前在这个应用程序上成功使用了send_data,这令人困惑.

我在spreadsheet.rubyforge.org上使用Rails v2.3,Ruby v1.8.7和Spreadsheet v6.4.1.

解决方法

只需更改行:
send_data data.string,:filename => @outfile

至:

send_data data.string.bytes.to_a.pack("C*"),:filename => @outfile

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读