首页 > 解决方案 > 使用ajax下载文件在firefox上不起作用

问题描述

我正在尝试使用 Ajax 下载生成的文件(pdf),它在 Chrome 和 IE 中运行良好,但在 FireFox 中却没有。这是我的代码:

function Download(urlAction, urlDownload) {

    $.ajax({
        type: "post",
        url: urlAction,
        data: {
            'itemIds': checkedItems,
            'dateMin': datemin.toISOString(),
            'dateMax': datemax.toISOString()
        },
        datatype: "json",
        traditional: true,
        success: function (data) {
            console.log('fff', data);
            if (data.success) {
                window.location = urlDownload;
            }
        }
        error: function (xhr, textStatus, err) {
            console.log("readyState: " + xhr.readyState);
            console.log("responseText: " + xhr.responseText);
            console.log("status: " + xhr.status);
            console.log("text status: " + textStatus);
            console.log("error: " + err);
        }
    });
}
}

在 UrlAction 中,我生成 Json 格式的文件并将其发布到会话中,然后在我的 urlDownload 中再次调用它。在 Chrome 和 IE 中,文件在不重新加载页面的情况下下载,但在 Firefox 中,它只重新加载页面。

错误说:

标签: c#ajaxasp.net-mvc

解决方案


对我有用的是我将按钮(调用此方法的按钮)从表单 div 中取出,它停止重新加载页面,下载就像一个魅力。


推荐阅读