【发布时间】:2021-07-06 02:37:06
【问题描述】:
我想下载已上传的.zip、.xlsm、xlsx、pdf……等已上传的文件,我想最终将它们压缩成一个zip文件以供下载。
但是,如果上传的文件没有被压缩,它们将被下载,其中存储着奇怪的文件。
在这种情况下,.xlsm
源代码
class DownloadZipsController < ::ApplicationController
def index
file_name = "#{Time.current.strftime("%Y%m%d_%H%M%S")}.zip"
zip_file = Tempfile.new(file_name)
Zip.unicode_names = true
Zip::OutputStream.open(zip_file.path) do |zip|
params[:product_ids].each do |product_id|
product = Product.find(product_id)
zip.put_next_entry("output.zip")
zip.print Net::HTTP.get URI.parse(product.submit_zip_file.url)
end
end
send_file zip_file.path,
type: "application/zip",
disposition: "attachment",
filename: file_name
zip_file.close
rescue => e
logger.debug e.backtrace
redirect_to request.referer, alert: e.message
end
end
上传的文件存储在 AWS S3 中。
有没有办法解决这个问题?
【问题讨论】:
-
您确实应该将此代码移出控制器并放入可以单独测试的单独服务对象中。
标签: ruby-on-rails ruby rubyzip