码迷,mamicode.com
首页 > 其他好文 > 详细

XHR post请求下载文件

时间:2020-04-03 16:29:19      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:nload   else   this   var   head   document   java   creat   XML   

            var filename = ‘test.xlsx‘;
            var xhr = new XMLHttpRequest();
            xhr.open(‘POST‘, downloadExcel, true);
            xhr.setRequestHeader("Content-Type", "application/json");
            xhr.responseType = ‘blob‘;
            xhr.onload = function(res) {
                if (this.status === 200) {
                    var type = xhr.getResponseHeader(‘Content-Type‘);
                    var blob = new Blob([this.response], {type: type});
                    if (typeof window.navigator.msSaveBlob !== ‘undefined‘) {
                        /*
                         * For IE
                         * >=IE10
                         */
                        window.navigator.msSaveBlob(blob, filename);
                    } else {
                        /*
                         * For Non-IE (chrome, firefox)
                         */
                        var URL = window.URL || window.webkitURL;
                        var objectUrl = URL.createObjectURL(blob);
                        if (filename) {
                            var a = document.createElement(‘a‘);
                            if (typeof a.download === ‘undefined‘) {
                                window.location = objectUrl;
                            } else {
                                a.href = objectUrl;
                                a.download = filename;
                                document.body.appendChild(a);
                                a.click();
                                a.remove();
                            }
                        } else {
                            window.location = objectUrl;
                        }
                    }
                }
            }
            xhr.send(JSON.stringify(list));

  

XHR post请求下载文件

标签:nload   else   this   var   head   document   java   creat   XML   

原文地址:https://www.cnblogs.com/jiehanshi/p/12627112.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!