简体   繁体   English

CSV导出-这是怎么回事?

[英]CSV export - what's going on here?

Using the following code to (try and) generate a CSV. 使用以下代码(尝试并)生成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!?! 我不确定发生了什么,运行此程序时,系统会提示我下载并在Excel中打开CSV(这是我想要的),但是里面什么也没有。...我还注意到文件保存在项目根目录(例如,名为“ schedule_23Feb12”),并且确实具有预期的内容! 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   

Sounds like a bug i've run into before with Internet Explorer. 听起来像是我以前使用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 ). 您所看到的可能是文档的缓存版本,或者甚至根本没有文档,因为它一下载就从IE的缓存中过期(如果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. 尝试将显式Cache-control: max-age=5为标题之一,以便获得文档的新副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM