XMLHttpRequest下載文件


轉自:https://blog.csdn.net/Angel_jn/article/details/108059927 (前端 XMLHttpRequest 實現下載excel文件)

       const xhr = new XMLHttpRequest();
            xhr.open('get', 'http://192.168.1.102:3333/');
            xhr.send();
            xhr.responseType = 'blob'; //設置請求回來的數據為blob方式
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    // 數據在 this.response 保存
                    // excel 的 MIME 格式為 application/vnd.ms-excel
                    var blob = new Blob([this.response], {
                        type: "application/vnd.ms-excel"
                    });
                    // 創建a鏈接 href鏈接地址 download為下載下來后文件的名稱
                    var aa = document.createElement('a');
                    aa.href = URL.createObjectURL(blob);
                    aa.innerHTML = 'a鏈接';
                    aa.download = 'aa.xls';
                    aa.style.display = 'none'; //隱藏a標簽 直接調用a標簽的點擊事件
                    document.body.appendChild(aa);
                    aa.click();
                }
            }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM