简体   繁体   中英

Jquery FileDownload doesn't trigger successCallback Event

I'm trying to implement a function that on Click event, Download a file, and close the UI dialog When the file download is finished. The problem is the, after $preparingFileModal.dialog({ modal: true }) the code doesn't trigger anymore and successCallback cannot detect the end fo file download .

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

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

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

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

Have a look at Jquery File download ($.fileDownload)

You need to set header "Set-Cookie: fileDownload=true; path=/"

header("Set-Cookie: fileDownload=true; path=/"); is how I did it in PHP. I start zipping some files when the user clicks on the download button. After the zip file is created, I set the header as above and echo the zip file path to the browser and start the download via jqueryFileDownload.

//set filedownload cookie.
header('Set-Cookie: fileDownload=true; path=/');
echo json_encode(array("OK" => "Zip file created", 'file' => $zipFileName));

如果您使用的HapiJS版本> = 15 ,则还需要指定isSecureisHTTPOnly标志

reply.state("fileDownload", "true", { path: "/" , isSecure: false, isHttpOnly: false })

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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM