2012-12-13 27 views
6

Tôi đang sử dụng tập tin jquery tải ..

mã được như sau:

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; 
} 

Đây là những gì tôi đang làm tại nút bấm

Khi tôi nhấp vào nút, PrepareMessageHtml: "Chúng tôi đang chuẩn bị báo cáo của bạn, vui lòng chờ ...", được hiển thị trong hộp thoại ... Vấn đề là ở đây hộp log không đi ra sau khi fle hoàn thành việc chuẩn bị của nó và tôi phải đóng nó bằng tay ... Làm thế nào tôi có thể làm cho nó đi ra khi tập tin hoàn thành việc chuẩn bị và đã sẵn sàng để tải về ..

Cảm ơn

Trả lời

16

để thực hiện jQuery biết việc tải xuống tập tin chỉ Đã xảy, đáp ứng tiêu đề của bạn phải chứa Set-Cookie: fileDownload=true; path=/.

Trong Java:

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

Thanx chúng ta phải thiết lập các cookie và cho trình duyệt khi tải xuống tệp sẽ bắt đầu – user1899841

+0

Bạn có thể xem http://stackoverflow.com/questions/21158481/execution-order-of-http-response-headers –

+0

Cảm ơn bạn người đàn ông !!!!!! Bạn cứu mạng tôi!! – sergioBertolazzo

3

đây là mã nguồn của tập tin jquery tải ..

$(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

Vào những thành công;

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

On lỗi

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

Sử dụng "Cache-Control" chỉ nếu ý chí tồn tại anothers yêu cầu.

0

Một số trình duyệt cũng cần bạn đặt lại phản hồi hoặc quá trình tải xuống của bạn sẽ không hoạt động!

response.reset(); 
0

hãy thử như thế này

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; 
     } 
    } 

referance từ: https://www.experts-exchange.com/questions/28713105/Jquery-fileDownload-successcallback-not-working.html

Tôi hy vọng nó làm việc cho bạn ..

Các vấn đề liên quan