CSV 导出 - 这是怎么回事?

发布于 2025-01-08 13:48:32 字数 669 浏览 2 评论 0原文

使用以下代码(尝试并)生成 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

格子衫的從容 2025-01-15 13:48:32

听起来像是我之前在使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文