xml监听下载进度

时间:2021-08-11
本文章向大家介绍xml监听下载进度,主要包括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();

原文地址:https://www.cnblogs.com/lhs-fight/p/15127295.html