对于我的Ruby on Rails项目(Rails版本5.1.2(,我正在生成图像文件(png(并使用RubyZip gem将它们下载为zip文件。
图像文件不存储在任何目录中。我有一个名为附件的模型。每个附件都有一个属性image_string,该属性是图像的 base64 字符串。您可以使用类似 image_tag(src = "data:image/jpeg;base64, #{attachment.image_string}", style: "border-radius: 0;")
的标记来显示图像
对于多个图像,我想为每个图像创建临时文件,而无需将它们存储在任何地方,并将这些图像下载为 zip 文件。
我现在拥有的代码:
def bulk_download
require('zip')
::Zip::File.open("/tmp/mms.zip", Zip::File::CREATE) do |zipfile|
Attachment.all.each do |attachment|
image_file = Tempfile.new("#{attachment.created_at.in_time_zone}.png")
image_file.write(attachment.image_string)
zipfile.add("#{attachment.created_at.in_time_zone}.png", image_file.path)
end
end
send_file "/tmp/mms.zip", type: 'application/zip', disposition: 'attachment', filename: "my_archive.zip"
respond_to do |format |
format.all { head :ok, content_type: "text/html" }
end
end
但是下载的zip文件中没有文件,大小为0字节。提前谢谢。
您应该需要关闭并取消链接 zip 文件,如下所示:
require('zip')
class SomeController < ApplicationController
# ...
def bulk_download
filename = 'my_archive.zip'
temp_file = Tempfile.new(filename)
begin
Zip::OutputStream.open(temp_file) { |zos| }
Zip::File.open(temp_file.path, Zip::File::CREATE) do |zip|
Attachment.all.each do |attachment|
image_file = Tempfile.new("#{attachment.created_at.in_time_zone}.png")
image_file.write(attachment.image_string)
zipfile.add("#{attachment.created_at.in_time_zone}.png", image_file.path)
end
end
zip_data = File.read(temp_file.path)
send_data(zip_data, type: 'application/zip', disposition: 'attachment', filename: filename)
ensure # important steps below
temp_file.close
temp_file.unlink
end
end
end
这是我用作此代码源代码的一篇很好的博客文章:https://thinkingeek.com/2013/11/15/create-temporary-zip-file-send-response-rails/
此外,最好将所有库要求保留在文件顶部(即 require('zip')
(。
接受的解决方案确实是正确的。但是,我将扩展已经提供的解决方案,使其与ActiveStorage
附件一起使用。
在使用公认的解决方案时,我发现image_string
方法不适用于ActiveStorage
附件并引发这样的错误
NoMethodError - undefined method `image_string' for #<ActiveStorage::Attached::One:0x00007f78cc686298>
假设我们有一个名为 Product
的 rails 模型,其 ActiveStorage 属性名为 attachment
class Product < ApplicationRecord
has_one_attached :attachment
end
为了使它适用于ActiveStorage附件,我们需要更新代码,如下所示
begin
Zip::OutputStream.open(temp_file) { |zos| }
Zip::File.open(temp_file.path, Zip::File::CREATE) do |zipfile|
Product.all.each do |product|
image_file = Tempfile.new("#{product.attachment.created_at.in_time_zone}.png")
# image_file.write(product.attachment.image_string) #this does not work for ActiveStorage attachments
# use this instead
File.open(image_file.path, 'w', encoding: 'ASCII-8BIT') do |file|
product.attachment.download do |chunk|
file.write(chunk)
end
end
zipfile.add("#{product.attachment.created_at.in_time_zone}.png", image_file.path)
end
end
zip_data = File.read(temp_file.path)
send_data(zip_data, type: 'application/zip', disposition: 'attachment', filename: filename)
ensure # important steps below
temp_file.close
temp_file.unlink
end
它对我有用(我需要加载基于Carrierwave的MyModel文档(:
require 'zip'
require 'open-uri'
class TestsController < ApplicationController
def index
filename = 'test.zip'
temp_file = ::Tempfile.new(filename)
my_model_document = ::MyModel.last
my_model_document_name = ::File.basename(my_model_document.document.path)
begin
::Zip::OutputStream.open(temp_file) { |zos| }
::Zip::File.open(temp_file.path, ::Zip::File::CREATE) do |zipfile|
dr_temp_file = Tempfile.new(my_model_document_name)
dr_temp_file.write(open(my_model_document.document.url).read.force_encoding("UTF-8"))
zipfile.add(my_model_document_name, dr_temp_file.path)
end
zip_data = File.read(temp_file.path)
send_data(zip_data, type: 'application/zip', disposition: 'attachment', filename: filename)
ensure
temp_file.close
temp_file.unlink
end
end
end