I have a file server which has to be deleted after a user downloads it. I send the file with send_file
, but I can't just put a File.delete(path)
after that line because send_file returns immediatly, so the file is deleted even before the user receives the complete file.
How do I fix this? I think it's a common problem but haven't found a solution yet.
Here is my code below:
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
Can Anyone please suggest me, how to delete a file from the server once the download is completed in the client side?