我正在尝试实现在单击事件,下载文件并在文件下载完成后关闭UI对话框的功能。
问题是,在$preparingFileModal.dialog({ modal: true })之后,该代码不再触发,并且successCallback无法检测到文件下载结束。

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

最佳答案

看看Jquery File download ($.fileDownload)

您需要设置标题"Set-Cookie: fileDownload=true; path=/"header("Set-Cookie: fileDownload=true; path=/");是我在PHP中所做的。当用户单击下载按钮时,我开始压缩一些文件。创建zip文件后,我按上述设置标题,并将zip文件路径回显到浏览器,然后通过jqueryFileDownload开始下载。

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

09-25 18:31