2

The problem lies in the following code ("routes.rb").

  post 'download_csr' do
    file_name = File.dirname(__FILE__) + '/cert.csr'
    File.open(file_name, 'w') do |f|
      f.write params[:csr]
    end
    send_file file_name, :disposition => 'attachment'
    erb :load_certificate, :locals => { :csr => 'cert.csr' }
  end

The flow is this: the user clicks a button on a form, then a file is written and downloaded to local system, then it goes to another page (i.e., "load_certificate.erb").

However, what really happens is that after the file is downloaded, it doesn't go to the next page. But if I comment out "send_file", it will go to the next page. So how to address this? Thanks a lot!

One more thing: it would be good to make sure the file is actually downloaded before going to the next page (because when send_file pops out a window, the user can choose "cancel"). Some time-out mechanism may not work here.

user180574
  • 5,681
  • 13
  • 53
  • 94
  • 1
    This might help you http://stackoverflow.com/questions/16859579/is-it-possible-to-redirect-to-new-page-after-send-file-method – Soup Dec 03 '13 at 06:26

1 Answers1

0

@Soup's link is a good one to read. With regard to Sinatra, send_file calls halt, which ends the request (in this case by sending the status and the file). Nothing will be processed after a halt.

ian
  • 12,003
  • 9
  • 51
  • 107