Jquery文件下载($.fileDownload)

Jquery文件下载($.fileDownload),jquery,download,Jquery,Download,我正在使用jquery文件下载 $(function () { $(document).on("click", "a.fileDownloadCustomRichExperience", function () { var $preparingFileModal = $("#preparing-file-modal"); $preparingFileModal.dialog({ modal: true }); $.fileDownlo

我正在使用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>
代码如下:

 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:“我们正在准备您的报告,请稍候…”。。。 问题是,fle完成准备后,此对话框不会关闭,我必须手动关闭它。。。 当文件完成准备并准备好下载时,如何使其启动


谢谢

这是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>
$(函数(){
$(文档)。在(“单击”,“a.fileDownloadCustomRichExperience”,函数()上{
var$preparingfilemodel=$(“#准备文件模式”);
$preparingFileModal.dialog({modal:true});
$.fileDownload($(this.attr('href'){
successCallback:函数(url){
$preparingFileModal.dialog('close');
},
failCallback:函数(responseHtml、url){
$preparingFileModal.dialog('close');
$(“#错误模式”).dialog({model:true});
}
});
return false;//这对于停止将触发正常文件下载的click事件至关重要!
});
});
我们正在准备您的报告,请稍候。。。
生成报告时出现问题,请重试。

为了让JQuery知道刚刚下载的文件,您的响应头必须包含
Set Cookie:fileDownload=true;路径=/

在Java中:

response.setHeader("Set-Cookie", "fileDownload=true; path=/");
成功论

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

仅当存在其他请求时才使用“缓存控制”。

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

response.reset();
请像这样试试

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;
        }
    }
函数exportToExcelTest(){
var region=$('#ddlRegion').val();
var hrinfo=$(“#hrinfo下拉列表”).val();
如果(区域!=null){
$(“#导出选项”).modal('hide');
$.blockUI({消息:“请等待生成excel数据…”);
//$.blockUI({消息:'稍等…});
$.blockUI({css:{backgroundColor:'#f00',color:'#fff'}});
var myData=region+':'+hrinfo;
$.fileDownload('Excel.ashx'{
httpMethod:“POST”,
数据:{data:myData},
successCallback:函数(url){
//$(“div#loading”).hide();
//警报(“正常”);
//setHeader(“Set Cookie”,“fileDownload=false;path=/”;
$.unbui();
},
prepareCallback:函数(url){
//警报(“正常”);
//setHeader(“Set Cookie”,“fileDownload=true;path=/”;
$.unbui();
},
failCallback:函数(responseHtml、url){
//警报(“正常”);
//$(“div#loading”).hide();
//警报(“生成excel文件时出错”);
//setHeader(“Set Cookie”,“fileDownload=false;path=/”;
$.unbui();
}
});          
}
否则{
警报(“请选择一个区域…”);
返回false;
}
}
推荐人:


我希望它对您有用。

Thanx我们必须设置Cookie,并告诉浏览器文件下载何时开始。您可能希望在asp.net mvc中查看,我使用了:Response.AppendHeader(“设置Cookie”,“fileDownload=true;path=/”;