XHR post请求下载文件
Posted jiehanshi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了XHR post请求下载文件相关的知识,希望对你有一定的参考价值。
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请求下载文件的主要内容,如果未能解决你的问题,请参考以下文章