gpt4 book ai didi

ruby-on-rails - 将回形针文件下载为 ZIP

转载 作者:行者123 更新时间:2023-12-04 20:52:14 25 4
gpt4 key购买 nike

其实我正在做这样的事情:

project          = Project.find(params[:id])  
attachments_list = project.attachments.where{attach_file_size > 0}
assets_list = project.assets.where{image_file_size > 0}
#Person.where{(name =~ 'Ernie%') & (salary < 50000) | (name =~ 'Joe%') & (salary > 100000)}
file_name = project.title.downcase.gsub(' ', '_destroy')
file_name = "#{file_name}.zip"

temp_file = Tempfile.new("#{file_name}-#{current_user.id}")
Zip::ZipOutputStream.open(temp_file.path) do |zos|
attachments_list.each do |file|
zos.put_next_entry(file.title)
zos.print IO.read(file.attach.path)
end
assets_list.each do |file|
zos.put_next_entry(file.title)
zos.print IO.read("#{file.image.path}")
end
end

send_file temp_file.path, :type => 'application/zip',
:disposition => 'attachment',
:filename => file_name
temp_file.close

它可以工作,但收到的文件中缺少扩展名,知道吗?

最佳答案

我终于在相关模型中创建了一个方法来返回带有扩展名的文件名

def title_with_ext
"#{self.title}#{File.extname(self.image.path)}"
end

关于ruby-on-rails - 将回形针文件下载为 ZIP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9263249/

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