尝试创建包含许多图片的zip文件,我的代码如下:
compressed_filestream = Zip::OutputStream.write_buffer do |zos|
@pictures.each do |p|
image_url = p.picturefile.url(:original)
zos.put_next_entry p.picturefile_file_name
zos.print(URI.parse(image_url))
end
end
compressed_filestream.rewind
send_data compressed_filestream.read, filename: "pictures.zip"
这会创建一个pictures.zip文件,但它只包含每个图片的text-url而不是实际图片......
答案 0 :(得分:3)
现在您只是将图像的URL传递给文件。您需要做的是将行内容传递给它。
compressed_filestream = Zip::OutputStream.write_buffer do |zos|
@pictures.each do |p|
zos.put_next_entry p.picturefile_file_name
zos.print(Paperclip.io_adapters.for(p.picturefile).read)
end
end
compressed_filestream.rewind
send_data compressed_filestream.read, filename: "pictures.zip"
答案 1 :(得分:0)
添加上面提供的解决方案,
如果您想在本地保存它并提供稍后或通过API
下载此zip文件的路径compressed_filestream = Zip::OutputStream.write_buffer do |zos|
@pictures.each do |pic|
zos.put_next_entry pic.upload_file_name
zos.write(Paperclip.io_adapters.for(pic.upload.url).read)
end
end
File.open("#{Rails.root}/public/system/#{file_name}", 'wb') { |f| f.write(compressed_filestream.string) }