【问题标题】:Ruby On Rails : Create csv-file and download as .zip-archiveRuby On Rails:创建 csv 文件并下载为 .zip-archive
【发布时间】:2015-10-16 06:17:21
【问题描述】:

您好,我创建了csv file,我想在我的rails 应用程序中下载.zip-file 中的那个文件。请帮我。我无法做到这一点,如何将该文件添加到 zip 并下载该 zip 文件。如何在 rails 中渲染该文件。

下面是我试过的一段代码,

     # method to create csv
     def to_csv
       CSV.generate do |csv|
         #build csv format of document
       end
     end

     # controller action to download zip
     def zip_download
       zip = Zip::File.open('document.zip', Zip::File::CREATE) do |zipfile|
               @documents.each do |document|
                # document.to_csv how to add this file .zip
               end
             end
       send_data zip, filename: "abc.zip", type: 'application/zip'
     end

【问题讨论】:

    标签: ruby-on-rails-4


    【解决方案1】:

    你可以试试:

    zip.get_output_stream("#{yourcsvname}.csv") { |f| f.puts(yourdocument.to_csv) }
    

    检查这个问题:Creating multiple csv-files and download all in one zip-archive using rails

    我认为这就是你所需要的。

    【讨论】:

      猜你喜欢
      • 2013-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      • 2018-01-03
      • 1970-01-01
      • 2011-01-16
      相关资源
      最近更新 更多