admin管理员组

文章数量:1606474

原生JS实现Ajax下载文件

/**
* url 下载url
* filename 下载文件名称
*/
function download(url, filename) {
  var xhr = new XMLHttpRequest();
  xhr.open('GET', url, true); 
  xhr.responseType = "blob"; // 设置返回类型为 blob
  
  // 设置请求头参数
  xhr.setRequestHeader('token', 'your_token_info');
  
  // 定义请求完成的处理函数,请求前也可以增加加载框/禁用下载按钮的相关逻辑
  xhr.onload = function() {
    if (this.status === 200) {
      var blob = this.response;
      var a = document.createElement('a');
      var url = window.URL.createObjectURL(blob);
      a.href = url;
      a.download = filename;
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
      window.URL.revokeObjectURL(url);
    }
  };
  xhr.send(); // 发送ajax请求
}

本文标签: 文件jsajax