我有一个sinatra应用程序,我想在其中进行下载功能.此下载从表中获取数据并为用户制作excel下载.
require 'csv'
get '/download' do
data = [{:name => "john", :age => 12, :state => 'ca'}, {:name => "tony", :age => 22, :state => 'va'}]
# I want to download this data as excel file and the content of file should be as follows:
# name,age,state
# john,12,ca
# tony,22,va
# I don't want to save data as a temp file on the server and then throw to user for download
# rather I want to stream data for download on the browser. So I used this,
# but it is not working
send_data (data, :type => 'application/csv', :disposition => 'attachment')
end
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?或者如何实现,我想做什么?我试图关注http://sinatra.rubyforge.org/api/classes/Sinatra/Streaming.html
更新:我没有和sinatra的send_data方法结婚.如果流式传输阻止我的服务器持续一段时间,那么我愿意接受替代方案.
get '/download' do
data = [{:name => "john", :age => 12, :state => 'ca'}, {:name => "tony", :age => 22, :state => 'va'}]
content_type 'application/csv'
attachment "myfilename.csv"
data.each{|k, v|
p v
}
end
Run Code Online (Sandbox Code Playgroud)
这适合我.我知道它不完整,因为我必须在带有换行符的excel文件中添加标题和逗号.但这很有效.
| 归档时间: |
|
| 查看次数: |
4125 次 |
| 最近记录: |