如何使用单击功能以相同的ajax调用成功下载多个文件?

时间:2019-05-16 07:04:52

标签: javascript jquery ajax

在我的文件中,我有2个具有href的锚标记。我在 ajax呼叫成功时点击了两个定位标记。

<a id="exportExcelFatturaIcon" href ="${createLink(action: 'downloadExcel', params: [fileName:excelFileName])}" hidden>click here</a>
<a id="exportCsvFatturaIcon" href ="${createLink(action: 'downloadCSV', params: [fileName:csvFileName])}"  hidden>click here</a>

Ajax呼叫:

$("#exportFatturaButton").click(function(){
    var startDate = $("#startDateFattura").val();
    var endDate =  $("#endDateFattura").val();
    $("#loaderModal").modal('show');
    $.ajax({
        url: "${createLink(controller: 'ExportData',action: 'getDataBySearch')}",
        data: {
            startDate: startDate,
            endDate:endDate
        },
        dataType: "html",
        type: "POST",
        success: function (data) {
            $("#loaderModal").modal('hide');
                document.getElementById("exportExcelFatturaIcon").click();
                document.getElementById("exportCsvFatturaIcon").click();
        },
        error: function (xhr, status) {
            $("#loaderModal").modal('hide');
        }, complete: function (xhr, status) {
            $("#loaderModal").modal('hide');
        }
    });
});

问题案例

情况1: 当我仅点击document.getElementById("exportExcelFatturaIcon").click();时,只会下载excel个文件。

案例2: 当我仅点击document.getElementById("exportCsvFatturaIcon").click();时,只会下载csv个文件。

情况3: 当我同时击中 document.getElementById("exportExcelFatturaIcon").click(); document.getElementById("exportCsvFatturaIcon").click();,然后将仅下载csv文件,而不是excel文件。

要求

但是在这种情况下3,我希望两个文件应同时下载,而不仅仅是一个。

1 个答案:

答案 0 :(得分:1)

尝试在

中代替触发成功回调中的click事件
window.open(document.getElementById("exportExcelFatturaIcon").href);
window.open(document.getElementById("exportCsvFatturaIcon").href);