Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 17th, 2012  |  syntax: None  |  size: 3.57 KB  |  hits: 58  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Streaming CSV Download from Rails 3.2 app
  2. class ExportsController < ApplicationController
  3.             require 'fastercsv'
  4.             require 'generic_agent'
  5.             require 'generic_record'
  6.  
  7.         def listing
  8.  
  9.           @this_filepath = "../html/whatever/" << Time.now.strftime("%I:%M:%S_%d:%m:%y") << ".csv"
  10.  
  11.           @spawn_id = spawn(:nice => 1) do
  12.  
  13.             FasterCSV.open(@this_filepath, "w") do |csv|
  14.  
  15.               csv << [ "outbreak_id"]
  16.             end
  17.  
  18.           end
  19.  
  20.           render :update do |page|
  21.             page.replace_html 'export_status', :partial => 'export_status_partial'
  22.           end
  23.  
  24.         end
  25.  
  26.   def send_export
  27.  
  28.     @this_filepath = params[:with]
  29.     csv_file = File.open(@this_filepath.to_s, 'r')
  30.  
  31.     csv_string = ""
  32.     csv_file.each_line do |line|
  33.       csv_string << line
  34.     end
  35.  
  36.    send_data csv_string, :filename => "export.csv",
  37.                 :type => 'text/csv; charset=iso-8859-1; header=present',
  38.                 :disposition => "attachment; filename=export.csv"
  39.                 #send_file @this_filepath.to_s, :stream => false, :type=>"text/csv", :x_sendfile=>true
  40.  
  41.                 #send_data csv_string, :filename => export.csv
  42.  
  43.                 #File.delete(@this_filepath.to_s)
  44.   end
  45.  
  46.   def export_checker
  47.     filename_array = params['filename'].split(///)
  48.                 @file_found = 0
  49.                 @file_ready = 0
  50.  
  51.                 @file_size = File.size(params['filename'])
  52.                 @this_filepath = params['filename']
  53.  
  54.                 if File.exists?(params['filename'])
  55.                 release_time = Time.now - 5.seconds
  56.                 if File.mtime(params['filename']).utc < release_time.utc
  57.  
  58.                 @file_found = 1
  59.                 @file_ready = 1
  60.                 @file_access_time = File.mtime(params['filename'])
  61.                 @file_release_time = release_time
  62.                 @file_size = File.size(params['filename'])
  63.  
  64.                 else
  65.                 @file_found = 1
  66.                 @file_ready = 0
  67.                 @file_size = File.size(params['filename'])
  68.  
  69.                 end
  70.  
  71.                 else
  72.  
  73.                 @file_found = 0
  74.                 @file_ready = 0
  75.                 @file_size = File.size(params['filename'])
  76.  
  77.                 end
  78.  
  79.     render :action => "export_checker"
  80.   end
  81. end
  82.        
  83. if @file_found == 1 && @file_ready == 1 && @file_size > 0
  84.  
  85.  
  86. page.replace_html 'link_to_file', :partial => "export_ready"
  87. if @file_release_time
  88. page.replace_html 'notice', "<div>Completed #{@file_release_time.strftime("%I:%M:%S %A %d %B %Y")} :: file size #{@file_size.to_s}</div>"
  89. end
  90.  
  91. page.visual_effect :highlight, 'link_to_file', :endcolor => '#D3EDAB'
  92.  
  93. elsif @file_found == 1
  94. page.replace_html 'link_to_file', "<div> File found, but still being constructed.</div><div>#{@this_filepath.to_s}</div>"
  95. page.visual_effect :highlight, 'link_to_file', :endcolor => '#FF9900'
  96. else
  97. page.replace_html 'link_to_file', "<div> File not found @file_found #{@file_found.to_s} @file_ready #{@file_ready.to_s}</div>"
  98. page.visual_effect :highlight, 'link_to_file', :endcolor => '#FF0000'
  99. end
  100.        
  101. format.csv {
  102.     @entries = Entry.all
  103.     @columns = ["First Name", "Last Name"].to_csv
  104.     @filename = "entries-#{Date.today.to_s(:db)}"
  105.  
  106.     self.response.headers["Content-Type"] ||= 'text/csv'
  107.     self.response.headers["Content-Disposition"] = "attachment; filename=#{@filename}"
  108.     self.response.headers["Content-Transfer-Encoding"] = "binary"
  109.  
  110.     self.response_body = Enumerator.new do |y|
  111.       @entries.each_with_index do |entry, i|
  112.         if i == 0
  113.           y << @columns
  114.         end
  115.         y << [entry.first_name, entry.last_name].to_csv
  116.       end
  117.     end
  118.   }