js原生方式post请求下载zip文件

做vue后台项目的时候需要post请求向后台提交多数据,方法如下

let url = window.g.ApiUrl + '/file/export/info';
var xhr = new XMLHttpRequest();
xhr.open('post', url, true);
xhr.responseType = 'blob';
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function () {
	if (xhr.readyState == 4 && xhr.status == 200) {
          var name = xhr.getResponseHeader('Content-disposition');
          var filename = name.substring(20, name.length);
          var blob = new Blob([xhr.response]);
          let link = document.createElement('a');
          let url = URL.createObjectURL(blob);
          link.style.display = 'none';
          link.href = url;
          link.download = filename;
          document.body.appendChild(link);
          link.click();
        }
}
xhr.send(JSON.stringfy(this.selectlist));
    原文作者:youlinaixu
    原文地址: https://blog.csdn.net/youlinaixu/article/details/89026227
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞