2014-10-03 210 views
1

我试图实现一个函数,在Click事件,下载文件,并关闭UI对话框当文件下载完成。 问题是,在$preparingFileModal.dialog({ modal: true })之后,代码不再触发,successCallback无法检测到文件下载结束。Jquery FileDownload不触发successCallback事件

$(function() { 
    $(document).on("click", "a.fileDownloadCustomRichExperience", function() { 

     var $preparingFileModal = $("#preparing-file-modal"); 

     $preparingFileModal.dialog({ modal: true }); 

     $.fileDownload($(this).prop('href'), { 
      successCallback: function (url) { 

       $preparingFileModal.dialog('close'); 
      }, 
      failCallback: function (responseHtml, url) { 

       $preparingFileModal.dialog('close'); 
       $("#error-modal").dialog({ modal: true }); 
      } 
     }); 
     return false; //this is critical to stop the click event which will trigger a normal file download! 
    }); 
}); 

<div id="preparing-file-modal" title="Preparing report..." style="display: none;"> 
    We are preparing your report, please wait... 

    <div class="ui-progressbar-value ui-corner-left ui-corner-right" style="width: 100%; height:22px; margin-top: 20px;"></div> 
</div> 

<div id="error-modal" title="Error" style="display: none;"> 
    There was a problem generating your report, please try again. 
</div> 

回答

7

看一看Jquery File download ($.fileDownload)

你需要设置页眉"Set-Cookie: fileDownload=true; path=/"

header("Set-Cookie: fileDownload=true; path=/");是我如何在PHP中做到了。当用户点击下载按钮时,我开始压缩一些文件。在创建zip文件后,我如上设置标题,并将浏览器的zip文件路径回显到浏览器,并通过jqueryFileDownload开始下载。

//set filedownload cookie. 
header('Set-Cookie: fileDownload=true; path=/'); 
echo json_encode(array("OK" => "Zip file created", 'file' => $zipFileName)); 
+0

这个cookie,你设置,我们需要启用的HttpOnly为这个? – Sid 2018-01-22 12:24:30

-1

请尝试像这样

function exportToExcelTest() { 
     var region = $('#ddlRegion').val(); 
     var hrinfo = $('#hrinfodropdown').val(); 
     if (region != null) { 
      $('#ExportOptions').modal('hide'); 
      $.blockUI({ message: '<h1>Please wait generating excel data...</h1>' }); 
      //$.blockUI({ message: '<h1><img src="../Images/ajax_loader_blue_350.gif" /> Just a moment...</h1>' }); 
      $.blockUI({ css: { backgroundColor: '#f00', color: '#fff'} }); 
      var myData = region + ':' + hrinfo; 

      $.fileDownload('Excel.ashx', { 
       httpMethod: "POST", 
       data: { data: myData }, 
       successCallback: function (url) { 
        //$("div#loading").hide(); 
        //alert('ok'); 
        //response.setHeader("Set-Cookie", "fileDownload=false; path=/"); 
        $.unblockUI(); 
       }, 
       prepareCallback: function (url) { 
        //alert('ok'); 
        //response.setHeader("Set-Cookie", "fileDownload=true; path=/"); 
        $.unblockUI(); 
       }, 
       failCallback: function (responseHtml, url) { 
        //alert('ok'); 
        // $("div#loading").hide(); 
        // alert('Error while generating excel file'); 
        //response.setHeader("Set-Cookie", "fileDownload=false; path=/"); 
        $.unblockUI(); 
       } 
      });   
     } 
     else { 
      alert('Please select a region....'); 
      return false; 
     } 
    } 

全球化志愿服务青年:https://www.experts-exchange.com/questions/28713105/Jquery-fileDownload-successcallback-not-working.html

我希望这是对你工作..