我有一个文件服务器在用户下载后必须删除。我发送的文件为send_file
,但我不能只在该行后面加上File.delete(path)
,因为send_file立即返回,所以即使在用户收到完整文件之前文件也会被删除。下载完成后从服务器上删除文件
我该如何解决这个问题?我认为这是一个常见问题,但还没有找到解决方案。
这里是我下面的代码:
def export_rawdata
@device = Device.find_by_id(params[:id])
@messages = @device.devmessages.all(:order => "id", :conditions => ["packettime >= ? and packettime <= ?", params[:start_time], params[:end_time]])
raw_data_path = "#{Rails.root}/tmp/exports/#{@device.s_dev_name}.csv"
FasterCSV.open(raw_data_path, "w+") do |csv|
csv << ["Packet","Created At"]
@messages.each_with_index do |m,i|
x = m.created_at
csv << [m.message, x.strftime('%h %d, %G %r')]
end
end
send_file raw_data_path, :type => "text/csv", :x_sendfile => true, :streaming => false
end
任何人都可以请建议我,如何从服务器删除文件一旦下载的客户端完成?
检查http://stackoverflow.com/questions/18232088/in-ruby-on-rails-after-send-file-method-delete-the-file-from-server – Nithin 2014-11-28 09:49:21