- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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/
我的代码中有以下两种方法。现在,我可以根据以下代码从 URL 读取 JSON。但是,我应该如何修改代码以便获得 gzip 文件,然后将其解码为 JSON? private static String
当我尝试使用 compc 构建 .swc 时,我得到了 Error: could not find a JVM。 我找到了 this站点,然后注释掉 java.home 行。那没有帮助。 我正在尝试从
1、此方法可以下载图片、压缩包、pdf(亲测),应该所有类型的文件都可以下载到本地,可以试一下 ?
我遇到了两个关于 zip 存档的问题,我想在这里分享并请求您的经验: 目录重复如果我在存档中创建两个或多个同名目录(在根目录下),存档包含两个目录(在查看条目时),但每个 ZIP 应用程序(也包括 W
我正在尝试将两个网络请求与 zip 函数结合起来。这两个请求依赖于另一个请求 (getInfoA)。 这是我正在使用的代码: IoC.networkProcessService.getInfoA(wi
我有我下载的文件: 2MGF&DPNA.part1.rar 2MGF&DPNA.part2.rar 2MGF&DPNA.part3.rar 2MGF&DPNA.part4.rar 2MGF&DPNA.
我需要使用 SharpCompress 将文件压缩为 7zip:http://sharpcompress.codeplex.com 我做了如下: using (var archive = ZipArc
.NET ZipArchive 是否允许重命名或移动条目?目前无法更改 ZipArchiveEntry 创建后的名称。看来我必须将原始 ZipArchiveEntry 的流复制到名称更改后的新 Zip
我是第一次尝试 Webpack。我已经将 Gulp 与 Browserify 一起使用了一段时间,并且对它非常满意。在这一点上,我只是在测试几个 Webpack 插件。即 compression-we
我正在尝试找出解决问题的方法;事实上,我正在编写自己的工具,使用 C++ 中的 libzip 来压缩文件来进行保存。 绝对没有完成,但我想做一些测试,然后我做了并从日志中获得了一个“有趣”的错误。 这
目录结构如下 Test app libs commons-codec.jar guava-r09.jar
我创建了一个简单的备份脚本,它只备份某些文件和文件夹。 tar -zcf $DIRECTORY/var.www.tar.gz /var/www tar -zcf $DIRECTORY/developm
我正在使用 Rails 5.2 和 ActiveStorage 让我的用户在我的应用程序中上传文件。我将所有上传的文件显示在一个表格中,我可以选择要下载的文件。选择后,我将能够将它们全部下载到一个 z
我正在使用 Rails 5.2 和 ActiveStorage 让我的用户在我的应用程序中上传文件。我将所有上传的文件显示在一个表格中,我可以选择要下载的文件。选择后,我将能够将它们全部下载到一个 z
我是一名优秀的程序员,十分优秀!