2014-10-03 209 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

我希望這是對你工作..