0
ファイルを含むzipファイルをダウンロードしようとしています。私はあなたが(私がコメントを翻訳)CSVをダウンロードしたい場合は私のコードは次のようになりますと言うこと、this tutorialを追っ:ここ"開いている間に読み込み用のエントリを開くことができません"というメッセージが表示される
def exporter_zip
# CSV's header
csv = "Title;Content;Publishing date;File name;\n"
# Loop over articles saved in database
Article.all.each do |article|
# Creating the CSV with datas
csv += "#{article.title};#{article.content};#{article.publishing_date.strftime("%Y-%m-%d") if article.publishing_date };#{article.file_name}\n"
end
# Creating the zip file inside the folder
zip_tmp = File.new("#{Rails.root}/db/mon_fichier.zip", "w+")
# opening the file in writing mode
Zip::File.open(zip_tmp.path, Zip::File::CREATE) {
|zipfile|
# Inserting the csv variable's content inside the article.csv file, which is inserted into the zip file
zipfile.get_output_stream("articles.csv") { |f| f.puts csv }
}
# Sending the created file
send_file "#{Rails.root}/db/mon_fichier.zip"
end
は、私はコードを適応する方法である:
class DownloadController < ApplicationController
require 'zip'
def zip
# Creating zip file
zip_tmp = File.new("#{Rails.root}/public/zip-#{Time.now.strftime('%d-%m-%Y')}.zip", 'w+')
FileDetail.all.each do |fichier|
Zip::File.open(zip_tmp.path, Zip::File::CREATE) { |zipfile|
# fichier.filename => file.mp3
# fichier.path => path/to/file.mp3
zipfile.get_output_stream(fichier.filename, fichier.path)
}
end
send_file "#{Rails.root}/public/zip-#{Date.today.to_time}.zip"
end
end
しかし、私がこれを正しく行っているかどうかは確かではありませんが、次のようなエラーが表示されます:Zip::File.open(zip_tmp.path, Zip::File::CREATE) { |zipfile|
何が起こっているか教えていただけますか?私は何が悪かったのか知らない前に、私は
はこの方法は、あなたのFileDetailの各メンバーのzipファイルを作成しようとし、それをやって、事前に
「get_output_stream」を「追加」に変更する必要がありましたが、今すぐ動作します。 – Jaeger