我正在使用 Rails 5.2 和 ActiveStorage 让我的用户在我的应用程序中上传文件。我将所有上传的文件显示在表格中,我可以选择要下载的文件。选择后,我将能够以 zip 文件形式下载所有这些内容。为了压缩文件,我使用 Rubyzip,但无法正常工作。
我尝试了两种方法:
1 - 我尝试了这种方法并遇到了此错误
No such file or directory @ rb_sysopen - /rails/active_storage/blobs/..../2234.py
这是我的控制器:
def batch_download
if params["record"].present?
ids = params["record"].to_unsafe_h.map(&:first)
if ids.present?
folder_path = "#{Rails.root}/public/downloads/"
zipfile_name = "#{Rails.root}/public/archive.zip"
FileUtils.remove_dir(folder_path) if Dir.exist?(folder_path)
FileUtils.remove_entry(zipfile_name) if File.exist?(zipfile_name)
Dir.mkdir("#{Rails.root}/public/downloads")
Record.where(id: ids).each do |attachment|
open(folder_path + "#{attachment.file.filename}", 'wb') do |file|
file << open("#{rails_blob_path(attachment.file)}").read
end
end
input_filenames = Dir.entries(folder_path).select {|f| !File.directory? f}
Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
input_filenames.each do |attachment|
zipfile.add(attachment,File.join(folder_path,attachment))
end
end
send_file(File.join("#{Rails.root}/public/", 'archive.zip'), …Run Code Online (Sandbox Code Playgroud)