javascript 下载 application/octet-stream 文件

function downloadFile(id) {
    var xhr = new XMLHttpRequest();
    xhr.open('POST', 'https://localhost/api/app/isp-detection/' + id + '/download');
    xhr.responseType = 'blob';
    xhr.setRequestHeader('Content-Type', 'application/octet-stream');
    xhr.onload = function () {
        if (xhr.status === 200) {
            var a = document.createElement('a');
            var url = window.URL.createObjectURL(xhr.response);
            console.log(url);
            a.href = url;
            a.download = id + '.xlsx';
            document.body.appendChild(a);
            a.click();
            window.URL.revokeObjectURL(url);
        }
    };
    xhr.send();
}

 

posted @ 2024-02-07 17:52  邪不压正!  阅读(185)  评论(0编辑  收藏  举报