我有一个简单的 sinatra 应用程序,它需要生成一个文件(通过外部进程),将该文件发送到浏览器,最后从文件系统中删除该文件。沿着这些线的东西:
class MyApp < Sinatra::Base
get '/generate-file' do
# calls out to an external process,
# and returns the path to the generated file
file_path = generate_the_file()
# send the file to the browser
send_file(file_path)
# remove the generated file, so we don't
# completely fill up the filesystem.
File.delete(file_path)
# File.delete is never called.
end
end
然而,似乎 send_file
调用完成了请求,并且它之后的任何代码都没有运行。
有没有办法确保生成的文件在成功发送到浏览器后被清理干净?或者我是否需要求助于在某个时间间隔运行清理脚本的 cron 作业?
最佳答案
不幸的是,当您使用 send_file 时没有任何回调。这里常见的解决方案是使用 cron 任务来清理临时文件
关于ruby - 通过 send_file 发送文件后,如何删除 Sinatra 中的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2806053/