Jquery 文件下载 ($.fileDownload)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13853300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:01:05  来源:igfitidea点击:

Jquery File download ($.fileDownload)

jquerydownload

提问by user1899841

I am using jquery file download ..

我正在使用 jquery 文件下载..

the code is as follows :

代码如下:

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

This is what i am doing at button click

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

When i click on the button , the preparingMessageHtml: "We are preparing your report, please wait...", is shown in a dialog box ... The problem is that this dialog box does not go off after the fle completes its preparing and i have to close it manually ... How can i make it go off when the file completes its preparation and is ready to download..

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

Thanks

谢谢

回答by Daniel Carroza

In order to make JQuery knows the file download just ocurred, your response header must contains Set-Cookie: fileDownload=true; path=/.

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

In Java:

在 Java 中:

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

回答by Dharmesh Hariyani

Here is source code of jquery file download ..

这是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>

回答by Caio Henrique

On sucess;

成功;

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

On error

出错时

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

Use "Cache-Control" only if will exists anothers requests.

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

回答by Alireza Fattahi

Some browsers also need you to reset response or your download will not work!

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

response.reset();

回答by Kaushik solanki

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

参考来源:https://www.experts-exchange.com/questions/28713105/Jquery-fileDownload-successcallback-not-working.html

I hope it's Work for you..

我希望它对你有用..