我有一個文件服務器在用戶下載後必須刪除。我發送的文件爲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