1

So on my page, I have a button where user can export the data to EXCEL/PDF. The way it works is they click on the button and then I send the data into a hidden form which submits the request. Once the request is done, it returns the content of the document type that the user selected which prompts the "Save As" dialog.

<form id="export-form" action="laravel-excel.php" method="POST" target="hidden-form">
    <input type="hidden" name="type" value="{{header.export.type}}" />
    <input type="hidden" name="name" value="{{header.export.name}}" />
    <input type="hidden" name="data" value="{{header.export.data}}" />
</form>
<iframe style="display:none" name="hidden-form"></iframe> 

So everything works as it should! However, I want to add a loader to let people know that the file is processing and when its done, I want to hide it. Well based on my research I was unable to find a solution that works for forms. The solutions I found are ones where the processing happens via AJAX like so:

$('#export-form').ajaxForm({
success: function (response, status, request) 
{
    var disp = request.getResponseHeader('Content-Disposition');
    if (disp && disp.search('attachment') != -1) 
    {
       var type = request.getResponseHeader('Content-Type');
       var blob = new Blob([response], { type: type });
       var URL = window.URL || window.webkitURL;
       var downloadUrl = URL.createObjectURL(blob);
        window.location = downloadUrl;
    }
}});

Is there a better solution out there?

Marin Petkov
  • 2,128
  • 4
  • 17
  • 23
  • http://stackoverflow.com/questions/1106377/detect-when-browser-receives-file-download – Igor May 06 '15 at 16:21
  • Haha that's a pretty clever solution. Would this be considered the actual solution or is it more of a hack? – Marin Petkov May 06 '15 at 17:01
  • Instead of applying heck why don't you make an ajax call and handle whatever you want in success response. – Ani May 06 '15 at 17:22
  • Because we need to fire the "Save As" dialog box. So as you can see in the description, the second code block is the AJAX solution that you are referring to. I just wanted to see if there is a better solution. – Marin Petkov May 06 '15 at 17:24
  • Btw, the reason the second solution is not preferable is because it is not supported on IE 9 or IE 8 (http://caniuse.com/#feat=bloburls) – Marin Petkov May 06 '15 at 17:27

0 Answers0