javascript - Jquery FileDownload 不触发 successCallback 事件

标签 javascript jquery jquery-ui

我正在尝试实现一个功能,在单击事件时下载文件,并在文件下载完成时关闭 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)

您需要设置 header “Set-Cookie: fileDownload=true; path=/”

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

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

关于javascript - Jquery FileDownload 不触发 successCallback 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26177265/

相关文章:

javascript - Angular UI Sortable 无法正常工作

javascript - 基本 Javascript 验证

javascript - 使用 location.reload() 后滚动到特定的 div

javascript - 在特定时间段内暂停javascript线程

javascript - 如何将html表单中的最小值和最大值设置为小数?

jquery - 调整浏览器大小时如何防止我的 div 重叠?

jquery - fullCalendar viewRender使用ajax无限循环

asp.net-mvc - MVC 数据注释范围验证无法正常工作

javascript - jQuery UI 可拖动 : Stack on Click

Javascript - 将 key 发送到正确动态创建的 div