如何从application / octet-stream数据创建文件?

时间:2018-05-04 15:32:55

标签: javascript jquery ajax rest

我有一个REST API,它将application / octet-stream作为MediaType返回,现在我需要创建一个文件并在客户端下载它!

$("#export-space-excel-button").click(function(e) {
    e.preventDefault();
    $("#export-excel-space-dialog").hide();
    $("#export-excel-space-progress-dialog").css("display","");
    $("#export-excel-space-progress-dialog").show();
    $.ajax({
        url :  AJS.contextPath() + "/export/excel/space/"+spaceKey,
        type : "GET",
        data : {data1:data1},
        responseType: 'application/octet-stream',
        contentType: 'application/json',
        success: function(data) {
            const url = window.URL.createObjectURL(new Blob([data]));
            const link = document.createElement('a');
            link.href = url;
            link.setAttribute('download', 'test.xlsx');
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
            $("#export-excel-space-progress-dialog").hide();
        }
    });    
});

0 个答案:

没有答案