8

我正在使用 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; 
}

这就是我在单击按钮时所做的

当我点击按钮时,preparingMessageHtml:“我们正在准备您的报告,请稍候......”,显示在一个对话框中......问题是文件完成准备后这个对话框没有消失我必须手动关闭它......当文件完成准备并准备下载时,我怎样才能让它消失..

谢谢

4

5 回答 5

22

为了让 JQuery 知道刚刚发生的文件下载,您的响应标头必须包含Set-Cookie: fileDownload=true; path=/.

在 Java 中:

response.setHeader("Set-Cookie", "fileDownload=true; path=/");
于 2012-12-14T14:05:17.653 回答
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>
于 2012-12-13T05:10:17.213 回答
1

成功;

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");

仅当存在其他请求时才使用“Cache-Control”。

于 2016-02-17T14:01:30.737 回答
0

有些浏览器还需要您重置响应,否则您的下载将无法正常工作!

response.reset();
于 2016-03-09T04:35:02.977 回答
0

please try like this

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 from : https://www.experts-exchange.com/questions/28713105/Jquery-fileDownload-successcallback-not-working.html

I hope it's Work for you..

于 2017-03-08T07:12:04.867 回答