gpt4 book ai didi

ruby - Sinatra 中临时文件的 send_file

转载 作者:数据小太阳 更新时间:2023-10-29 07:39:42 25 4
gpt4 key购买 nike

我正在尝试使用 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 系统也能正常工作。

最佳答案

我的猜测是您的一首歌曲名称中有拼写错误,或者 song.url 的最后一部分中有一个斜线?我 adopted your code如果所有歌曲都存在,则将 zip 作为临时文件发送就可以了。

关于ruby - Sinatra 中临时文件的 send_file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15418175/

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