我有一个简单的sinatra应用程序需要生成一个文件(通过外部进程),将该文件发送到浏览器,最后从文件系统中删除该文件。沿着这些路线的东西:但是如何在通过send_file发送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作业?
这不会使用更多的内存吗? – James 2012-06-21 18:24:04
-1,如果该文件长度为4GB,该怎么办? – yeyo 2014-10-27 03:39:12
@Kira,发送一个4GB的文件到浏览器? 'generate_the_file()'方法应该阻止这种情况。我的建议是更改操作顺序,以便在浏览器接收文件之前删除生成的文件...原始问题。 – 2014-11-03 18:16:13