xml监听下载进度

const xhr = new XMLHttpRequest();
        xhr.open('GET', `/open/planbook/downloadExcel/${id}`);
        xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        xhr.responseType = 'blob';
        xhr.onprogress = function (event) {
            if (event.lengthComputable) {
                console.log(event.loaded, '下载结束');
                console.log(event.total, 'total====');
            }
        };
        xhr.onload = function (oEvent) {
            console.log(oEvent, 'oEvent===');
            console.log(xhr.status, 'status===');
            console.log(xhr.response, 'response===');
            if (xhr.readyState === 4 && xhr.status === 200) {
                // var name = xhr.getResponseHeader("Content-disposition");
                // var filename = name.substring(20,name.length);
                const blob = new Blob([xhr.response]);
                const csvUrl = URL.createObjectURL(blob);
                const link = document.createElement('a');
                link.href = csvUrl;
                link.download = 'download.xlsx';
                link.click();
            }
        };
        xhr.send();

 

posted @ 2021-08-11 11:01  二双  阅读(83)  评论(0编辑  收藏  举报