如何在Ruby on Rails中下载CSV文件?

21

在我的InvoicesController里,我有这样的代码:

def index
  @invoices = current_user.invoices
  respond_to do |format|
    format.html
    format.xls
    format.csv # not working!
  end
end

在我的index.html.erb视图中,我有以下两个下载链接:

<%= link_to "Download as Excel", invoices_path(:format => "xsl") %>
<%= link_to "Download as CSV", invoices_path(:format => "csv") %>

模板index.xsl.erbindex.csv.erb也存在。

第一个链接有效,即Excel文件被下载到用户的计算机中。但是CSV文件在浏览器中呈现而不是下载。

我该怎么做才能让用户也下载CSV文件?

感谢任何帮助。


你是否在初始化程序中引用了Ruby的本地CSV库? - zeantsoi
5个回答

33

尝试指定适当的内容头 并且 在您的format.csv处理程序块中明确呈现您的index.csv.erb模板。

# app/controllers/invoices_controller.rb
format.csv do
    response.headers['Content-Type'] = 'text/csv'
    response.headers['Content-Disposition'] = 'attachment; filename=invoice.csv'    
    render :template => "path/to/index.csv.erb"
end

1
谢谢!那个方法起作用了。看来你必须明确指定模板名称,即使我花了一些时间才弄清语法:render "#{Rails.root}/app/views/invoices/index.csv.erb" 我已经相应地修改了你的答案。 - Tintin81
2
很高兴听到它起作用了。这是一个迂腐的问题,但是你可以验证一下 render :template => 'invoices/index'(不带路径)是否也可以工作?据我所知应该可以,我会相应地更新我的答案。 - zeantsoi
我还必须添加 :layout => false - Usagi

6

试试这个

format.csv do
  response.headers["Content-Type"] = "text/csv; charset=UTF-8; header=present"
  response.headers["Content-Disposition"] = "attachment; filename=invoices.csv"
end

谢谢!我尝试了,但是它没有下载或者在浏览器中显示文件。当我点击链接时什么也没发生。我也在上面添加了我的 index.csv.erb 文件的内容。 - Tintin81

2

最近我发现

render_csv

也许你可以看看这个railscast(赞)


0

我找到了适合我的另一种解决方案(Rails 3.2和Ruby 2.3.3)

  1. 生成CSV之后
report_csv = CSV.generate(col_sep: ";")...
  1. 可以从控制器中下载:
  • 在块 format.csv 中,我们可以使用
send_data(report_csv, filename: 'banana.csv')

我们可以通过一个带有 target blank 的链接访问这个操作。

0

ERB主要用于生成非结构化数据格式和标记。我个人不会使用它来生成CSV,因为内置的CSV库就是为此而设计的。

在更近期的Rails版本中,有一个输出流,它允许您直接写入输出以减少总体内存使用。

所以,也许可以像这样做?

require 'csv'

class BooksController
  def index
    response.content_type = Mime[:csv]
    response.headers['Content-Disposition'] = content_disposition
    # Write directly to the response stream so the client can start reading
    # immediately when the web server starts flushing the buffer.
    response.stream.write CSV.generate_line(header)
    # Use find_each to prevent loading all records into memory.
    Book.find_each.each do |book|
      # Immediately write rows to the response stream to reduce memory usage.
      response.stream.write CSV.generate_line(row(book)) }
    end
  ensure
    response.stream.close
  end

  private

  def header
    # Rely on the model and I18n to format attribute names.
    %i[id title updated].map do |attribute_name|
      Book.human_attribute_name(attribute_name)
    end
  end

  def row(book)
    [book.id, book.title, book.updated_at.iso8601]
  end

  def content_disposition
    # Use ContentDisposition to allow characters in the filename which are
    # normally not allowed in an HTTP header value (ie. all non-ASCII and
    # certain control characters).
    ActionDispatch::Http::ContentDisposition.format(
      disposition: 'attachment', filename: "#{content_filename}.csv"
    )
  end

  def content_filename
    "All books present at the café in #{Time.zone.today.year}"
  end
end

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接