[Ruby] IO。不读取整个文件

时间:2014-03-22 17:18:11

标签: ruby-on-rails ruby file io

我想通过rails方法下载包含一些图像的zip文件。问题是当我尝试读取文件时,我没有得到整个文件。当然,显示图像非常烦人。

这是我的代码:

  def download_zip(image_list)
    if !image_list.blank?
      file_name = "pictures.zip"
      t = Tempfile.new("lolbite11")
      Zip::OutputStream.open(t.path) do |z|
        image_list.each do |img|
          title = img.name
          title += ".jpg" unless title.end_with?(".jpg")
          z.put_next_entry(title)
          z.print IO.read(Rails.root.join('public', img.path)) <- Here is the problem ?
        end
      end
      send_file t.path, :type => 'application/zip',
                :disposition => 'attachment',
                :filename => file_name
      t.close
    end
  end

我下载了zip并将其解压缩后。我用文本编辑器打开我的图像,遗憾地看到我不是整个图像...我试图输出&#39; IO.read&#39;并且它只显示我最终文件中的内容。

1 个答案:

答案 0 :(得分:0)

我终于通过File.copy_stream()找到了另一种解决问题的方法。

但如果有人知道为什么这不起作用,请随时向我解释。

相关问题