js post下载文件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>下載</title>
<script>
var xmlhttp = getXMLHttpRequest();
? ? var record = "747140577079087276";
? ? var url = "http://xxx.com/controller/download?docId=" + record;
? ? xmlhttp.open("POST", url, true);
? ? xmlhttp.responseType = 'blob';
? ? xmlhttp.setRequestHeader("Content-Type","application/json");
? ? xmlhttp.setRequestHeader("Authorization","bearer 6870332e-6741-461b-840f-6e49fcce3baf");
? ? xmlhttp.send();
? ? xmlhttp.onreadystatechange = function() {
? ? ? ? if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
? ? ? ? ? ? console.log('111');
? ? ? ? ? ? saveAs(xmlhttp.response, record + ".xlsx");
? ? ? ? }
? ? };
function getXMLHttpRequest() {
? ? var xmlhttp = null;
? ? if (window.XMLHttpRequest) {
? ? ? ? xmlhttp = new XMLHttpRequest();
? ? } else {
? ? ? ? xmlhttp = new ActiveXObject("Microsoft.XMLHttp");
? ? }
? ? return xmlhttp;
}
function saveAs(data, name) {
? ? const blob = new Blob([data]);
? ? const aElement = document.createElement('a');
? ? const blobUrl = window.URL.createObjectURL(blob);
? ? aElement.href = blobUrl;
? ? aElement.download = name;
? ? aElement.click();
? ? window.URL.revokeObjectURL(blobUrl);
}
</script>
</head>
<body>
</body>
</html>
總結
以上是生活随笔為你收集整理的js post下载文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 创新案例分享 | 临床研究数据信息互联互
- 下一篇: Nodecache免费香港CDN加速DN