1

In my view I have a link_to for downloading an excel file:

<%= link_to 'Download Excel', url_for( :controller => "my_controller", :action => "file", :format => 'xlsx', :params => params ) %>

The controller uses axlsx to render the file like:

  format.xlsx {
    render xlsx: 'file', filename: 'filename', disposition: 'inline'
  }

Now it can take a bit of time to generate and return that file, so I'd like to give the user an indication that the site's working.

I have a hidden div in the view code:

<div id="loader"><img src="/assets/loading.gif" ></div>

When someone clicks on the link, I can show the div with this jQuery:

$('#excel_export').click(function() { 
  $("#loader").show();
});

My question is: how can I .hide() that div when the file download starts? Maybe an ajax callback of some sort?

scim
  • 55
  • 1
  • 7

2 Answers2

1

I don't know if this may help you. I found this code helpful to show spinner while an ajax request is performed.

$(document)
    .ajaxStart(function() {
        $("#loader").show();
    })
    .ajaxStop(function() {
        $('#loader').hide();
    });

You can get more samples from the following link.

Community
  • 1
  • 1
Deepak A
  • 322
  • 1
  • 3
  • 12
0

You can use setTimeout() function by checking general time taken for download to start

Like this

$('#excel_export').click(function() { 
  $("#loader").show();
  setTimeout($("#loader").hide();, 1000);
});

So it will be automatically close after 1000ms

Jeet
  • 1,350
  • 1
  • 15
  • 32
  • Thanks, but the report won't necessarily take exactly one second to process. – scim Nov 19 '13 at 13:28
  • This must be general execution time... you can use 5 seconds or 10 seconds... whatever you want – Jeet Nov 19 '13 at 13:37