The approach I used with Rails 2.3.8 was to create a new thread to handle csv parsing, and then use an AJAX call to check the server to see if the file was ready (I relied on File.mtime )
Just pulled it out of the application to post it here, so I removed alot of csv parsing code and didn't include all the views
sorry the end of the day rush: D
Controllers / exports_controller.rb
class ExportsController < ApplicationController require 'fastercsv' require 'generic_agent' require 'generic_record' def listing @this_filepath = "../html/whatever/" << Time.now.strftime("%I:%M:%S_%d:%m:%y") << ".csv" @spawn_id = spawn(:nice => 1) do FasterCSV.open(@this_filepath, "w") do |csv| csv << [ "outbreak_id"] end end render :update do |page| page.replace_html 'export_status', :partial => 'export_status_partial' end end def send_export @this_filepath = params[:with] csv_file = File.open(@this_filepath.to_s, 'r') csv_string = "" csv_file.each_line do |line| csv_string << line end send_data csv_string, :filename => "export.csv", :type => 'text/csv; charset=iso-8859-1; header=present', :disposition => "attachment; filename=export.csv"
view / export / export_checker.rjs
if @file_found == 1 && @file_ready == 1 && @file_size > 0 page.replace_html 'link_to_file', :partial => "export_ready" if @file_release_time page.replace_html 'notice', "<div>Completed #{@file_release_time.strftime("%I:%M:%S %A %d %B %Y")} :: file size #{@file_size.to_s}</div>" end page.visual_effect :highlight, 'link_to_file', :endcolor => '#D3EDAB' elsif @file_found == 1 page.replace_html 'link_to_file', "<div> File found, but still being constructed.</div><div>#{@this_filepath.to_s}</div>" page.visual_effect :highlight, 'link_to_file', :endcolor => '#FF9900' else page.replace_html 'link_to_file', "<div> File not found @file_found #{@file_found.to_s} @file_ready #{@file_ready.to_s}</div>" page.visual_effect :highlight, 'link_to_file', :endcolor => '#FF0000' end
source share