2012-12-13 58 views
6

我使用jQuery文件下載..

的代碼如下:

function downloadFile(){ 
var downloadOptions = { 
preparingMessageHtml: "We are preparing your report, please wait...", 
failMessageHtml: "No reports generated. No Survey data is available." 
        }; 

    $.fileDownload("displaySurveyReport.action",downloadOptions); 
return false; 
} 

這就是我在做的按鈕點擊

當我點擊按鈕,prepareMessageHtml:「我們正在準備您的報告,請稍候...」,顯示在一個對話框中... 問題是,這個dia在FLE完成其準備登錄後箱不走了,我必須手動關閉它... 我怎樣才能使當文件完成其準備並準備下載它熄滅..

感謝

回答

16

爲了使JQuery的知道只是內容時發生的文件下載,你的響應頭必須包含Set-Cookie: fileDownload=true; path=/

在Java:

response.setHeader("Set-Cookie", "fileDownload=true; path=/"); 
+0

感謝名單,我們必須設置的cookie,並告訴瀏覽器時,文件下載將開始 – user1899841

+0

你可能想看看http://stackoverflow.com/questions/21158481/execution-order-of-http-response-headers –

+0

謝謝你這個人!!!!!!你救了我的命! – sergioBertolazzo

3

這裏是jQuery的文件下載的源代碼..

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

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

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

     $.fileDownload($(this).attr('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> 
1

在sucess;

response.setHeader("Set-Cookie", "fileDownload=true; path=/"); 
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); 

在錯誤

response.setHeader("Set-Cookie", "fileDownload=false; path=/"); 
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); 

使用 「緩存控制」 只有意志存在anothers請求。

0

某些瀏覽器還需要您重置響應或者您的下載不起作用!

response.reset(); 
0

請嘗試像這樣

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

我希望這是對你工作..