【问题标题】:Converting binary IOstream into file将二进制 IOstream 转换为文件
【发布时间】:2011-01-07 14:54:06
【问题描述】:

我正在使用 Rails 服务器。我正在发送核心 http 请求。 在 request.body 内容中,我要上传一个文件。这个 request.body 是 StringIo 对象。我想将此文件上传到我的服务器。

【问题讨论】:

  • 您能详细说明一下吗?如果您已经编写了一些代码并试图开始工作,我们肯定希望看到它。

标签: ruby-on-rails ruby upload


【解决方案1】:

这会将文件以 1mb (1024**2) 块的形式写入磁盘。一次读入整个文件可能会让你打开一个包含大量文件的 DOS。

File.open("where-you-want-the-file", "w") do |f|
  while blk = request.body.read(1024**2)
    f << blk
  end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    相关资源
    最近更新 更多