Creating multiple csv-files and download all in one zip-archive using rails

六月ゝ 毕业季﹏ 提交于 2019-12-03 15:41:33

You could either write your CSV output into a temporary file and call zipfile.add() on that, but there is a cleaner solution:

zipfile.get_output_stream("#{user.name}.csv") { |f| f.puts(user.to_csv) }

See http://rdoc.info/github/rubyzip/rubyzip/master/Zip/File#get_output_stream-instance_method for more details on get_output_stream - you can also pass additional parameters to specify attributes for the file to be created.

get_output_stream doesn't work for me. However, the updated method Zip::OutputStream.write_buffer helps

https://gist.github.com/aquajach/7fde54aa9bc1ac03740feb154e53eb7d

The example adds password protection to the file as well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!