【发布时间】:2020-06-15 17:35:05
【问题描述】:
我有一个 Redmine 插件。我在 /tmp 中创建一个临时文件,然后使用 File.open 发送它。我想在用户下载临时文件时删除它。我该怎么办?
我的代码(在控制器中):
File.open(filelocation, 'r') do |file|
send_file file, :filename => filename, :type => "application/pdf", :disposition => "attachment"
end
如果我在 File.open 之后删除文件,它就不起作用。
编辑
在我的控制器中我这样做:
def something
temp = Tempfile.new(['PDF_','.pdf'])
# ... some code that modify my pdf ...
begin
File.open(temp.path, 'r') do |file|
send_file file, :filename => temp.path, :type => "application/pdf", :disposition => "attachment"
end
ensure
temp.close
temp.unlink
end
end
我的临时文件已删除,但不在我的代码末尾:File.open 返回损坏的 PDF。
【问题讨论】: