CSV 导出 - 这是怎么回事?
使用以下代码(尝试并)生成 CSV。我不确定发生了什么,当我运行此程序时,我收到下载并下载的提示。在 Excel 中打开 CSV(这就是我想要的),但其中没有任何内容....我还注意到项目根目录中保存了一个文件(例如名为“schedule_23Feb12”),并且它确实具有预期的内容!? !随后的重新运行会导致下载重复,但根文件被覆盖。
有什么想法吗?
def schedulecsv
@products = Product.where('release_date > ?', Date.today)
filename ="schedule_#{Date.today.strftime('%d%b%y')}"
csv_data = CSV.generate filename do |csv|
csv << ["cat_no","version"]
@products.each do |p|
csv << [p.cat_no,p.version]
end
end
send_data csv_data,
:type => 'text/csv; charset=iso-8859-1; header=present',
:disposition => "attachment; filename=#{filename}.csv"
end
Using the following code to (try and) generate a CSV. I'm not sure what's happening, when I run this I get a prompt to download & open the CSV in Excel (which is what I want) but it has nothing in it....I also noticed a file is saved in the project root (named 'schedule_23Feb12' for example) and this does have the expected content!?! Subsequent re-runs cause the download to be duplicated but the root file to be overwritten.
Any ideas?
def schedulecsv
@products = Product.where('release_date > ?', Date.today)
filename ="schedule_#{Date.today.strftime('%d%b%y')}"
csv_data = CSV.generate filename do |csv|
csv << ["cat_no","version"]
@products.each do |p|
csv << [p.cat_no,p.version]
end
end
send_data csv_data,
:type => 'text/csv; charset=iso-8859-1; header=present',
:disposition => "attachment; filename=#{filename}.csv"
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来像是我之前在使用 Internet Explorer 时遇到的一个错误。您看到的可能是文档的缓存版本,或者更糟糕的是根本没有文档,因为它在下载后就从 IE 缓存中过期了(如果
Cache-control: max-age=0
就会发生这种情况) )。尝试将显式的
Cache-control: max-age=5
设置为标头之一,以便获得文档的最新副本。Sounds like a bug i've run into before with Internet Explorer. What you're seeing could be either a cached version of the document or worse no document at all because it expired from IE's cache as soon as it downloaded (happens if
Cache-control: max-age=0
).Try setting an explicit
Cache-control: max-age=5
as one of your headers so you get fresh copies of the document.