【发布时间】:2013-03-03 07:51:40
【问题描述】:
我正在尝试使用 Sinatra 的内置 send_file 命令,但它似乎不适用于临时文件。
我基本上会执行以下操作来压缩 mp3 专辑:
get '/example' do
songs = ...
file_name = "zip_test.zip"
t = Tempfile.new(['temp_zip', '.zip'])
# t = File.new("testfile.zip", "w")
Zip::ZipOutputStream.open(t.path) do |z|
songs.each do |song|
name = song.name
name += ".mp3" unless name.end_with?(".mp3")
z.put_next_entry(name)
z.print(open(song.url) {|f| f.read })
p song.name + ' added to file'
end
end
p t.path
p t.size
send_file t.path, :type => 'application/zip',
:disposition => 'attachment',
:filename => file_name,
:stream => false
t.close
t.unlink
end
当我使用t = File.new(...) 时,一切正常,但我不想使用File,因为它会出现并发问题。
当我使用t = Tempfile.new(...) 时,我得到:
!! Unexpected error while processing request: The file identified by body.to_path does not exist`
编辑:看起来问题的一部分是我正在发送多个文件。如果我只发送一首歌曲,Tempfile 系统也能正常工作。
【问题讨论】:
-
如果您使用包含多个文件的预构建 zip 文件而不是使用
Zip::ZipOutputStream.open... 会发生什么?另外,send_file sends a halt 所以不需要t.close和t.unlink(无论如何文件系统都应该为你处理这个问题)。