【问题标题】:JS - window.open blob downloads file instead of opening new tab in FirefoxJS - window.open blob 下载文件而不是在 Firefox 中打开新标签
【发布时间】:2022-07-22 21:48:40
【问题描述】:

我有以下代码来获取 PDF 文件并在新选项卡中打开它:

$(document).on('click', '#downloadInvoice', function () {
    showLoader();
    $.ajax({
        type: 'POST',
        url: _baseUrl + 'orders/downloadinvoice/' + $(this).data('id'),
        xhrFields: {
            responseType: 'blob'
        },
        success: (response) => {
            const blob = new Blob([response], { type: 'application/pdf' }),
                  url  = window.URL.createObjectURL(blob)

            window.open(url);
        },
        error: () => {
            toastr.error('Error!');
        },
        complete: () => {
            hideLoader();
        }
    });
});

它在 Chrome 中按预期工作,但在 Firefox 中,它会自动下载文件,我必须在 Downloads 中查找它并手动打开它。

是 Firefox 阻止了新标签页的打开,还是我需要在我的代码中添加一些内容?奇怪的是,我在网上没有找到与此问题相关的任何内容。

【问题讨论】:

标签: javascript firefox


【解决方案1】:

使用window.open函数的第二个参数,设置为_blank

window.open(url, '_blank');

window.open

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-09
    • 2021-04-24
    • 2012-09-01
    • 2021-07-17
    • 2015-06-11
    • 2012-04-14
    • 2020-12-10
    相关资源
    最近更新 更多