HTML 只打开一个包含下载响应的选项卡

HTML only open one tab contain download response

我正在尝试创建一个按钮,该按钮在按下时触发下载一些文件。 这是我的 js 代码:

$menu.find('li').click(function () {
    var selectedData = that.getAllSelections();
    let data = {
        'order_numbers': []
    };
    selectedData.forEach(element => {
        data['order_numbers'].push(element['ordernumber']);
    });
    $.ajax({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        url: window.location.pathname + '/check/pdf',
        data: data,
        method: 'post',
        success: function (result) {
            if (result['status'] == 200) {
                result['data'].forEach(orderNumber => {
                    let downloadUrl = window.location.pathname + '/download/pdf/' + orderNumber;
                    window.open(downloadUrl, '_blank');
                });
                displayError(result['message'], 'success');
            } else {
                displayError(result['message']);
            }
        },
        error: function (error) {
            displayError('Something went wrong. Please try again');
        }
    });
});

这是它的工作原理。单击按钮时,它可以获得要下载的文件名列表。然后我将通过 ajax 调用 api 来检查这些文件是否存在。这 api 将 return 一个文件名数组存在。下一步我使用 for 循环打开大量选项卡,每个选项卡调用 api 将下载相应的文件。

我的问题是,当我点击下载按钮时,只打开了一个选项卡,因此只下载了 1 个文件。我在浏览器中检查了网络,ajax return 调用的 api 响应成功。我还逐个文件检查了所有文件都可以下载。

谁能指出问题出在哪里?

谢谢。

而不是调用 window.open(downloadUrl, '_blank') 传递常量名称作为第二个参数。 这样,its content will be replaced.

If a window with the name already exists, then url is loaded into the existing window.

我刚找到问题出在哪里。这是浏览器的问题,当打开太多标签时它会自动阻止。只需更改设置即可。