gpt4 book ai didi

ruby-on-rails - ruby 压缩包 : Unable to find path of file stored in Active Storage

转载 作者:行者123 更新时间:2023-12-02 00:24:38 26 4
gpt4 key购买 nike

我正在使用 Rails 5.2 和 ActiveStorage 让我的用户在我的应用程序中上传文件。我将所有上传的文件显示在一个表格中,我可以选择要下载的文件。选择后,我将能够将它们全部下载到一个 zip 文件中。为了压缩文件,我使用的是 Rubyzip,但无法正常工作。

我试过两种方法:
1 - 我试过 this way并遇到此错误
No such file or directory @ rb_sysopen -/rails/active_storage/blobs/..../2234.py
这是我的 Controller :

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'), :type => 'application/zip', :filename => "#{Time.now.to_date}.zip")
end
else
redirect_back fallback_location: root_path
end
end

2 - 其次,我尝试关注 rubyzip documentation并且错误有点不同。
没有这样的文件或目录@rb_file_s_lstat -/rails/active_storage/blobs/..../2234.py

if ids.present?
folder = []
input_filenames = []
Record.where(id: ids).each do |attachment|
input_filenames.push("#{attachment.file.filename}")
pre_path = "/rails/active_storage/blobs/"
path_find = "#{rails_blob_path(attachment.file)}"
folder.push(pre_path + path_find.split('/')[4])
end
container = Hash[folder.zip(input_filenames)]

zipfile_name = "/Users/fahimabdullah/Documents/archive.zip"

Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
# input_filenames.each do |filename|
container.map do |path, filename|
zipfile.add(filename, File.join(path, filename))
end
zipfile.get_output_stream("myFile") { |f| f.write "myFile contains just this" }
end

我希望它下载一个包含所有文件的 zip 文件。这是我的第一个问题,如果问题太长,请原谅。谢谢。

最佳答案

我刚刚遇到了类似的问题,但看到这个问题还没有得到解答。尽管它有点老,你可能已经解决了这个问题,但这是我的方法。这里的技巧是在两者之间创建一个临时文件

def whatever
zip_file = Tempfile.new('invoices.zip')

Zip::File.open(zip_file.path, Zip::File::CREATE) do |zipfile|
invoices.each do |invoice|
next unless invoice.attachment.attached?

overlay = Tempfile.new(['overlay', '.pdf'])
overlay.binmode
overlay.write(invoice.attachment.download)
overlay.close
overlay.path

zipfile.add(invoice.filename, File.join(overlay.path))
end
end

invoices_zip = File.read(zip_file.path)

UserMailer.with(user: user).invoice_export(invoices_zip, 'invoices.zip').deliver_now
ensure
zip_file.close
zip_file.unlink
end

关于ruby-on-rails - ruby 压缩包 : Unable to find path of file stored in Active Storage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54209861/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com