Rails3 生成和流式传输大文件

发布于 2024-09-24 07:59:10 字数 379 浏览 0 评论 0原文

我的 Rails3 应用程序有一个特定的请求,该请求会动态生成一个大的 CSV (20mb+),然后提示用户下载该文件(在生成文件时)。

我注意到在从 Rails 2.3.x 升级时使用“render :text => proc”不再有效。我现在正在研究一种解决方案,让我可以让事情以类似的方式进行。

该文件是动态生成的,这一事实尚未造成任何麻烦。只有某些用户可以访问此功能(例如月度报告等)。

我基本上计划采用两种不同的方法, 1.添加一个自定义渲染器,这将让我再次使用过程 2. 在线程中生成文件,并使用“send_data”尝试在生成文件时对其进行流式传输。

我还没有实现,所以如果有人可以提供一些见解,它可能有助于在代码升级时澄清问题。

谢谢!!!

my Rails3 Application has a certain request which generates a large CSV (20mb+) on the fly, and then prompts the user to download the file (while it is being generated).

I've noticed while upgrading from Rails 2.3.x using "render :text => proc" no longer works. I am now working on a solution that will let me keep things working in a similar fashion.

The fact that the file is being generated on the fly has not caused any pain just yet. Only certain users, can access this feature (think monthly reports, etc).

I am basically planning on two different approaches,
1. add a custom renderer which will let me use a proc again
2. generate the file in a thread, and use "send_data" to try and stream the file as it is being generated.

I haven't implemented yet, so if anyone can provide some insight, it may help clear things up as the code is upgraded.

Thanks!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

情话难免假 2024-10-01 07:59:11

我正在解决一个非常相似的问题,我决定分叉 csv_builder 以添加流支持。我有一个工作原型版本,它有一个模板处理程序返回一个过程而不是一个字符串。在过程内部,我连接 csv_builder 以直接写入输出流。我必须使用一些愚蠢的技巧才能使其在测试和开发中发挥作用,因为 TestResponse 的输出不是 CSV 期望的成熟的 io 流。

看一下:https://github.com/fawce/csv_builder

I'm working on a very similar problem, and I decided to fork csv_builder to add streaming support. I have a working prototype version, which has a template handler return a proc rather than a string. Inside the proc, I wire the csv_builder to write to the output stream directly. There are some goofy tricks I had to use to make it work in test and development, because TestResponse's output isn't the full-blown io stream that CSV expects.

take a look: https://github.com/fawce/csv_builder

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